Complete folder layout reorg

This commit is contained in:
MaMe82 2018-07-29 20:18:06 +02:00
parent 4e2470adef
commit 471b3b853e
599 changed files with 184 additions and 54315 deletions

View File

@ -1,16 +1,63 @@
all: build
# make dep runs without sudo
dep:
sudo apt-get -y install git screen hostapd autossh bluez bluez-tools bridge-utils policykit-1 genisoimage iodine haveged
sudo apt-get -y install tcpdump
sudo apt-get -y install python-pip python-dev
# before installing dnsmasq, the nameserver from /etc/resolv.conf should be saved
# to restore after install (gets overwritten by dnsmasq package)
cp /etc/resolv.conf /tmp/backup_resolv.conf
sudo apt-get -y install dnsmasq
sudo /bin/bash -c 'cat /tmp/backup_resolv.conf > /etc/resolv.conf'
# python dependencies for HIDbackdoor
sudo pip install pycrypto # already present on stretch
sudo pip install pydispatcher
# install go
wget https://storage.googleapis.com/golang/go1.9.linux-armv6l.tar.gz
sudo tar -C /usr/local -xzf go1.9.linux-armv6l.tar.gz
export PATH=$PATH:/usr/local/go/bin # put into ~/.profile
echo export PATH=$PATH:/usr/local/go/bin >> ~/.profile
sudo bash -c 'echo export PATH=\$PATH:/usr/local/go/bin >> ~/.profile'
# install gopherjs
go get -u github.com/gopherjs/gopherjs
# we don't need protoc + protoc-grpc-web, because the proto file is shipped pre-compiled
# go dependencies for webapp (without my own)
#go get google.golang.org/grpc
#go get -u github.com/improbable-eng/grpc-web/go/grpcweb
#go get -u github.com/gorilla/websocket
build:
go build P4wnP1_service.go
go build P4wnP1_cli.go
go get -u github.com/mame82/P4wnP1_go # partially downloads again, but we need the library packages in go path to build
env GOBIN=$(pwd)/build go install ./... # compile all main packages to the build folder
# compile the web app
# ToDo: (check if dependencies have been fetched by 'go get', even with the build js tags)
gopherjs build -m -o build/webapp.js web_client/*.go
install:
cp P4wnP1_service /usr/local/bin/
cp P4wnP1_cli /usr/local/bin/
cp P4wnP1.service /etc/systemd/system/P4wnP1.service
mkdir /usr/local/P4wnP1
cp -R keymaps /usr/local/P4wnP1/
cp -R scripts /usr/local/P4wnP1/
cp build/P4wnP1_service /usr/local/bin/
cp build/P4wnP1_cli /usr/local/bin/
cp dist/P4wnP1.service /etc/systemd/system/P4wnP1.service
# copy over keymaps, scripts and www data
mkdir -p /usr/local/P4wnP1
cp -R dist/keymaps /usr/local/P4wnP1/
cp -R dist/scripts /usr/local/P4wnP1/
cp -R dist/HIDScripts /usr/local/P4wnP1/
cp -R dist/www /usr/local/P4wnP1/
cp build/webapp.js /usr/local/P4wnP1/www
cp build/webapp.js.map /usr/local/P4wnP1/www
# careful testing
#sudo update-rc.d dhcpcd disable
#sudo update-rc.d dnsmasq disable
# reinit service daemon
systemctl daemon-reload
# enable service
@ -26,6 +73,9 @@ remove:
rm -f /usr/local/bin/P4wnP1_service
rm -f /usr/local/bin/P4wnP1_cli
rm -f /etc/systemd/system/P4wnP1.service
rm -R /usr/local/P4wnP1/
rm -R /usr/local/P4wnP1/ # this folder should be kept, if only an update should be applied
# reinit service daemon
systemctl daemon-reload
#sudo update-rc.d dhcpcd enable

31
Makefile.old Normal file
View File

@ -0,0 +1,31 @@
all: build
build:
go build P4wnP1_service.go
go build P4wnP1_cli.go
install:
cp P4wnP1_service /usr/local/bin/
cp P4wnP1_cli /usr/local/bin/
cp P4wnP1.service /etc/systemd/system/P4wnP1.service
mkdir /usr/local/P4wnP1
cp -R keymaps /usr/local/P4wnP1/
cp -R scripts /usr/local/P4wnP1/
# reinit service daemon
systemctl daemon-reload
# enable service
systemctl enable P4wnP1.service
# start service
service P4wnP1 start
remove:
# stop service
service P4wnP1 stop
# disable service
systemctl disable P4wnP1.service
rm -f /usr/local/bin/P4wnP1_service
rm -f /usr/local/bin/P4wnP1_cli
rm -f /etc/systemd/system/P4wnP1.service
rm -R /usr/local/P4wnP1/
# reinit service daemon
systemctl daemon-reload

View File

@ -1,9 +0,0 @@
package main
import (
"github.com/mame82/P4wnP1_go/cli_client"
)
func main() {
cli_client.Execute()
}

View File

@ -1,79 +0,0 @@
package main
import (
"log"
"./service"
"os"
"os/signal"
"syscall"
"fmt"
pb "github.com/mame82/P4wnP1_go/proto"
"github.com/mame82/P4wnP1_go/common"
"time"
"strconv"
)
func main() {
err := service.InitGlobalServiceState()
if err != nil { panic(err) }
state := service.ServiceState
state.StartService()
//ToDo: Check for root privs
err = service.CheckLibComposite()
if err != nil {
log.Fatalf("Couldn't load libcomposite: %v", err)
}
//service.NewLed(false) //Set LED to manual trigger
//service.InitDefaultLEDSettings()
/*
log.Printf("Keyboard devFile: %s\n", service.HidDevPath[service.USB_FUNCTION_HID_KEYBOARD_name])
log.Printf("Mouse devFile: %s\n", service.HidDevPath[service.USB_FUNCTION_HID_MOUSE_name])
log.Printf("HID RAW devFile: %s\n", service.HidDevPath[service.USB_FUNCTION_HID_RAW_name])
*/
service.StartRpcServerAndWeb("0.0.0.0", "50051", "80", "/home/pi/P4wnP1_go/www") //start gRPC service
//Indicate servers up with LED blink count 1
state.Led.SetLed(&pb.LEDSettings{1})
//service.StartEventManager(20)
log.SetOutput(state.EvMgr)
log.Println("TESTMESSAGE")
go func() {
err := common.RunBashScript("/usr/local/P4wnP1/scripts/servicestart.sh")
if err != nil { log.Printf("Error executing service startup script: %v\n", err) }
}()
//Send some log messages for testing
textfill := "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea"
i := 0
go func() {
for {
//println("Sending log event")
state.EvMgr.Emit(service.ConstructEventLog("test source", i%5, "message " +strconv.Itoa(i) + ": " + textfill))
time.Sleep(time.Millisecond *2000)
i++
}
}()
//use a channel to wait for SIGTERM or SIGINT
fmt.Println("P4wnP1 service initialized, stop with SIGTERM or SIGINT")
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s := <-sig
log.Printf("Signal (%v) received, ending P4wnP1_service ...\n", s)
state.StopService()
return
}

View File

@ -1,27 +0,0 @@
# USB gadget RPC mechanincs
- RPC server is in `service.go`
- RPC client with CLI is in `cli_client.go`
## RPC server
- holds state of desired USB setting with name `settingsState` and type `proto.GadgetSettings`
- `settingsState` is initialized to the hard-coded default settings when the RPC server is started (init) and deployed to the kernel via config FS
- the active USB gadget has not the same setting as the state stored in `settingsState`, this is only the case on init, when the default settings are deployed
- `settingsState` is meant to store the desired settings, which are customized by the user via RPC calls, in order to deploy the settings, an additional RPC call is needed (`deploySettings`)
- the user can change a single or multiple USB gadget settings with a single RPC call from the cli_client
- for this purpose, the RPC call `SetGadgetSettings` is used, which has to provide a full `proto.GadgetSettings` struct
- the settings provided with `SetGadgetSettings` overwrite the settings stored in `settingsState` (RPC doesn't keep track of changed and unchanged settings)
- thus, in order to change only a part of the settings, the client has to retrieve current `settingsState` via the RPC call `GetGadgetSettings` modify the result to its needs and send it back with `SetGadgetSettings`
- once the client is done with changing settings (could be done with a single or multiple calls to `SetGadgetSettings`) he issues the RPC call `deploySettings` to deploy the settings (real active seetings and `settingsState` are the same again, if no error occures)
- `GetDeployedGadgetSetting` RPC call could be used to retrieve the real settings in use from the running gadget
## Error handling
- the RPC server checks the settings to be valid after every call of `SetGadgetSettings`, if an error occures it is reported back and the `settingsState`isn't updated
- if `deploySettings` is called, the server stores the old settings of the gadgets, in case of an error it is reported back and the active gadget configuration, as well as the `settingsState`, are reverted to the stored configuration
- before Deploying new gadget settings, they are compared to the active ones, to avoid re-building of the composite gadget if it isn't needed
- the most likely reason to end up with invalid gadget settings is that too many USB gadgets functions are enabled simultaneously, as each function consumes one or more USB endpoints (there're only 7 EPs available)
## Additional notes
- the reason for not directly applying changed gadget settings, is that the whole composite gadget is destroyed and brought up again for every change
- there're likely parameters which could be changed without destroying the gadget (like the image backing an USB Mass Storage), these parameters are handled differently
- Re-deployment of the USB gagdget with changed settings interrupts the workflow, for example USB-based network interfaces are shut down during re-deployment (even if they aren't affected by the changed settings) - this would interrupt running connections or listening server sockets

View File

@ -7,9 +7,10 @@ import (
"errors"
"fmt"
"google.golang.org/grpc/status"
"github.com/mame82/P4wnP1_go/service"
//"github.com/mame82/P4wnP1_go/service"
"strings"
"strconv"
"github.com/mame82/P4wnP1_go/common"
)
//Empty settings used to store cobra flags
@ -233,7 +234,7 @@ func createDHCPServerSettings(iface string, ip4 string, mask4 string, disabled b
Enabled: !disabled,
DhcpServerSettings: &pb.DHCPServerSettings{
ListenInterface:iface,
LeaseFile: service.NameLeaseFileDHCPSrv(iface),
LeaseFile: common.NameLeaseFileDHCPSrv(iface),
CallbackScript: "",
ListenPort: 0, //Disable DNS
DoNotBindInterface: false, //only listen on given interface

View File

@ -0,0 +1,6 @@
# Common functionality used by "service" and "cli_client"
The folder hold consts, data and functions, which are used by both, the cli_client and the service.
It should not hold functionality used by the webclient, in order to avoid using huge packages like "fmt".
Functionality shared with the web_client has to be stored in the folder "common_web".

View File

@ -1 +1,11 @@
package common
import "fmt"
// File holds functions used by CLI client and P4wnP1 systemd service. Not to cross import the whole CLI/service
// package, is preferred over placing these functions in a contextual logic place.
func NameLeaseFileDHCPSrv(nameIface string) (lf string) {
return fmt.Sprintf("/tmp/dnsmasq_%s.leases", nameIface)
}

View File

@ -1,4 +1,4 @@
package common
package common_web
const (
EVT_ANY = int64(0)

View File

@ -1,6 +1,5 @@
# Common functionality used by "service" and "cli_client"
The folder hold consts, data and functions, which are used by both, the cli_client and the service.
It should not hold functionality used by the webclient, in order to avoid using huge packages like "fmt".
Functionality shared with the web_client has to be stored in the folder "common_web".
The folder hold consts, data and functions, which are used by the cli_client, the service AND/OR the web_service.
Using huge packages like "fmt" should not be used by functionality in this folder, in order to avoid importing it into the webclient.

View File

Some files were not shown because too many files have changed in this diff Show More