diff --git a/Dockerfile b/Dockerfile index 99f1ebbb..746b6364 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ #first stage - builder -FROM golang:1.14-stretch as builder +FROM golang:latest as builder COPY . /app @@ -10,22 +10,28 @@ ENV GO111MODULE=auto RUN CGO_ENABLED=0 GOOS=linux go build -o app main.go +WORKDIR /app/netclient + +ENV GO111MODULE=auto + +RUN CGO_ENABLED=0 GOOS=linux go build -o netclient main.go #second stage -FROM alpine:latest +FROM debian:latest + +RUN apt-get update && apt-get -y install systemd procps WORKDIR /root/ -RUN apk add --no-cache tzdata - COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=builder /app . COPY --from=builder /app/config config +COPY --from=builder /app/netclient netclient EXPOSE 8081 EXPOSE 50051 -CMD ["./app", "--clientmode=off"] +CMD ["./app"] diff --git a/controllers/dnsHttpController.go b/controllers/dnsHttpController.go index 3f8ebcb3..59b5bf67 100644 --- a/controllers/dnsHttpController.go +++ b/controllers/dnsHttpController.go @@ -441,7 +441,7 @@ func WriteHosts() error { } } } - err = hostfile.SaveAs("./config/netmaker.hosts") + err = hostfile.SaveAs("./config/dnsconfig/netmaker.hosts") return err } diff --git a/docker-compose.yml b/docker-compose.yml index 84c481d3..ea79912e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,8 @@ -version: "3.3" +version: "3.4" +volumes: + dnsconfig: + driver: local services: mongodb: image: mongo:4.2 @@ -13,24 +16,54 @@ services: MONGO_INITDB_ROOT_USERNAME: mongoadmin MONGO_INITDB_ROOT_PASSWORD: mongopass netmaker: + privileged: true container_name: netmaker + build: netmaker depends_on: - mongodb - image: gravitl/netmaker:v0.2 + image: gravitl/netmaker:v0.3 ports: - "8081:8081" - "50051:50051" + volumes: + - ./:/local + - /etc/netclient:/etc/netclient + - dnsconfig:/root/config/dnsconfig + - /usr/bin/wg:/usr/bin/wg:ro + - /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket + - /run/systemd/system:/run/systemd/system + - /etc/systemd/system:/etc/systemd/system + - /sys/fs/cgroup:/sys/fs/cgroup environment: - MONGO_HOST: mongodb + MONGO_HOST: "127.0.0.1" + SERVER_DOMAIN: "3.236.149.180" + cap_add: + - NET_ADMIN + - SYS_MODULE restart: always + network_mode: host netmaker-ui: container_name: netmaker-ui depends_on: - netmaker - image: gravitl/netmaker-ui:v0.2 + image: gravitl/netmaker-ui:v0.3 + links: + - "netmaker:api" ports: - "80:80" environment: - BACKEND_URL: "http://localhost:8081" + BACKEND_URL: "http://3.236.149.180:8081" + coredns: + depends_on: + - netmaker + image: coredns/coredns + command: -conf /root/dnsconfig/Corefile + container_name: coredns + restart: always + ports: + - "53:53/udp" + volumes: + - dnsconfig:/root/dnsconfig volumes: mongovol: {} + dnsconfig: {} diff --git a/functions/helpers.go b/functions/helpers.go index afd112ab..a2860730 100644 --- a/functions/helpers.go +++ b/functions/helpers.go @@ -49,11 +49,6 @@ func CreateServerToken(netID string) (string, error) { privAddr = network.LocalRange } - fmt.Println("Token details:") - fmt.Println(" grpc address + port: " + address) - fmt.Println(" network: " + netID) - fmt.Println(" private range: " + privAddr) - accessstringdec := address + "|" + netID + "|" + accesskey.Value + "|" + privAddr accesskey.AccessString = base64.StdEncoding.EncodeToString([]byte(accessstringdec)) @@ -131,8 +126,6 @@ func NetworkExists(name string) (bool, error) { if err == mongo.ErrNoDocuments { return false, nil } - fmt.Println("Error Retrieving Group: " + name) - fmt.Println(err) } return true, err } diff --git a/functions/local.go b/functions/local.go new file mode 100644 index 00000000..ed18502a --- /dev/null +++ b/functions/local.go @@ -0,0 +1,53 @@ +package functions + +import ( + "fmt" + "path/filepath" + "log" + "os" + "io/ioutil" +) + + +func FileExists(f string) bool { + info, err := os.Stat(f) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + +func ConfigureDNS() error { + dir, err := filepath.Abs(filepath.Dir(os.Args[0])) + if err != nil { + return err + } + _, err = os.Stat(dir + "/config/dnsconfig") + if os.IsNotExist(err) { + os.Mkdir(dir +"/config/dnsconfig", 744) + } else if err != nil { + fmt.Println("couldnt find or create /config/dnsconfig") + return err + } + + if !FileExists(dir + "/config/dnsconfig/Corefile") { + + corefile := `. { + hosts /root/dnsconfig/netmaker.hosts { + fallthrough + } + forward . 8.8.8.8 8.8.4.4 + log +} +` + corebytes := []byte(corefile) + + err = ioutil.WriteFile(dir + "/config/dnsconfig/Corefile", corebytes, 0644) + if err != nil { + log.Println(err) + log.Println("") + return err + } + } + return err +} diff --git a/main.go b/main.go index 44c65c61..98bd186d 100644 --- a/main.go +++ b/main.go @@ -15,10 +15,7 @@ import ( "go.mongodb.org/mongo-driver/bson" "fmt" "time" - "net/http" "strings" - "errors" - "io/ioutil" "os" "os/exec" "net" @@ -39,10 +36,12 @@ var PortGRPC string func main() { + var dnsmode string var clientmode string var defaultnet string flag.StringVar(&clientmode, "clientmode", "on", "Have a client on the server") flag.StringVar(&defaultnet, "defaultnet", "on", "Create a default network") + flag.StringVar(&dnsmode, "dnsmode", "on", "Add DNS settings") flag.Parse() if clientmode == "on" { @@ -77,6 +76,12 @@ func main() { } } } + if dnsmode == "on" { + err := functions.ConfigureDNS() + if err != nil { + fmt.Printf("Error setting DNS: %v", err) + } + } var waitnetwork sync.WaitGroup if config.Config.Server.AgentBackend { @@ -116,7 +121,7 @@ func runGRPC(wg *sync.WaitGroup, installserver bool) { PortGRPC = grpcport if os.Getenv("BACKEND_URL") == "" { if config.Config.Server.Host == "" { - ServerGRPC, _ = getPublicIP() + ServerGRPC, _ = serverctl.GetPublicIP() } else { ServerGRPC = config.Config.Server.Host } @@ -281,35 +286,6 @@ func createDefaultNetwork() (bool, error) { } - -func getPublicIP() (string, error) { - - iplist := []string{"https://ifconfig.me", "http://api.ipify.org", "http://ipinfo.io/ip"} - endpoint := "" - var err error - for _, ipserver := range iplist { - resp, err := http.Get(ipserver) - if err != nil { - continue - } - defer resp.Body.Close() - if resp.StatusCode == http.StatusOK { - bodyBytes, err := ioutil.ReadAll(resp.Body) - if err != nil { - continue - } - endpoint = string(bodyBytes) - break - } - - } - if err == nil && endpoint == "" { - err = errors.New("Public Address Not Found.") - } - return endpoint, err -} - - func authServerUnaryInterceptor() grpc.ServerOption { return grpc.UnaryInterceptor(controller.AuthServerUnaryInterceptor) } diff --git a/netclient/functions/common.go b/netclient/functions/common.go index 70783809..38c1ac4b 100644 --- a/netclient/functions/common.go +++ b/netclient/functions/common.go @@ -72,7 +72,7 @@ func GetFreePort(rangestart int32) (int32, error){ return portno, err } -func Install(accesskey string, password string, server string, network string, noauto bool, accesstoken string, inputname string) error { +func Install(accesskey string, password string, server string, network string, noauto bool, accesstoken string, inputname string, pubip string) error { tserver := "" tnetwork := "" @@ -252,6 +252,9 @@ func Install(accesskey string, password string, server string, network string, n } fmt.Println(" Local Address: " + localaddress) + if pubip != "" && pubip != "nopubip" { + endpoint = pubip + } else { if nodecfg.Endpoint == "" { if islocal && localaddress != "" { endpoint = localaddress @@ -269,6 +272,7 @@ func Install(accesskey string, password string, server string, network string, n endpoint = nodecfg.Endpoint fmt.Println("Endpoint set in config. Setting to address: " + endpoint) } + } fmt.Println(" Endpoint: " + endpoint) diff --git a/netclient/functions/local.go b/netclient/functions/local.go index c2962655..e16de7d9 100644 --- a/netclient/functions/local.go +++ b/netclient/functions/local.go @@ -118,40 +118,32 @@ WantedBy=timers.target return err } } - sysExec, err := exec.LookPath("systemctl") + //sysExec, err := exec.LookPath("systemctl") - cmdSysEnableService := &exec.Cmd { + cmdSysEnableService := exec.Command("systemctl", "enable", "netclient@.service")/*&exec.Cmd { Path: sysExec, Args: []string{ sysExec, "enable", "netclient@.service" }, Stdout: os.Stdout, Stderr: os.Stdout, - } - /* - cmdSysStartService := &exec.Cmd { - Path: sysExec, - Args: []string{ sysExec, "start", "netclient@.service"}, - Stdout: os.Stdout, - Stderr: os.Stdout, - } - */ - cmdSysDaemonReload := &exec.Cmd { + }*/ + cmdSysDaemonReload := exec.Command("systemctl", "daemon-reload")/*&exec.Cmd { Path: sysExec, Args: []string{ sysExec, "daemon-reload"}, Stdout: os.Stdout, Stderr: os.Stdout, - } - cmdSysEnableTimer := &exec.Cmd { + }*/ + cmdSysEnableTimer := exec.Command("systemctl", "enable", "netclient-"+network+".timer")/*&exec.Cmd { Path: sysExec, Args: []string{ sysExec, "enable", "netclient-"+network+".timer" }, Stdout: os.Stdout, Stderr: os.Stdout, - } - cmdSysStartTimer := &exec.Cmd { + }*/ + cmdSysStartTimer := exec.Command("systemctl", "start", "netclient-"+network+".timer")/*&exec.Cmd { Path: sysExec, Args: []string{ sysExec, "start", "netclient-"+network+".timer"}, Stdout: os.Stdout, Stderr: os.Stdout, - } + }*/ err = cmdSysEnableService.Run() if err != nil { @@ -191,7 +183,7 @@ func isOnlyService(network string) (bool, error) { } func RemoveSystemDServices(network string) error { - sysExec, err := exec.LookPath("systemctl") + //sysExec, err := exec.LookPath("systemctl") fullremove, err := isOnlyService(network) @@ -199,36 +191,36 @@ func RemoveSystemDServices(network string) error { fmt.Println(err) } - cmdSysDisableService := &exec.Cmd { + cmdSysDisableService := exec.Command("systemctl","disable","netclient@.service")/* &exec.Cmd { Path: sysExec, Args: []string{ sysExec, "disable", "netclient@.service"}, Stdout: os.Stdout, Stderr: os.Stdout, - } - cmdSysDaemonReload := &exec.Cmd { + }*/ + cmdSysDaemonReload := exec.Command("systemctl","daemon-reload")/*&exec.Cmd { Path: sysExec, Args: []string{ sysExec, "daemon-reload"}, Stdout: os.Stdout, Stderr: os.Stdout, - } - cmdSysResetFailed := &exec.Cmd { + }*/ + cmdSysResetFailed := exec.Command("systemctl","reset-failed")/*&exec.Cmd { Path: sysExec, Args: []string{ sysExec, "reset-failed"}, Stdout: os.Stdout, Stderr: os.Stdout, - } - cmdSysStopTimer := &exec.Cmd { + }*/ + cmdSysStopTimer := exec.Command("systemctl", "stop", "netclient-"+network+".timer")/*&exec.Cmd { Path: sysExec, Args: []string{ sysExec, "stop", "netclient-"+network+".timer" }, Stdout: os.Stdout, Stderr: os.Stdout, - } - cmdSysDisableTimer := &exec.Cmd { + }*/ + cmdSysDisableTimer := exec.Command("systemctl", "disable", "netclient-"+network+".timer")/*&exec.Cmd { Path: sysExec, Args: []string{ sysExec, "disable", "netclient-"+network+".timer"}, Stdout: os.Stdout, Stderr: os.Stdout, - } + }*/ //err = cmdSysStopService.Run() if err != nil { diff --git a/netclient/main.go b/netclient/main.go index 86247f09..f90cfee9 100644 --- a/netclient/main.go +++ b/netclient/main.go @@ -39,6 +39,7 @@ func main() { tname := flag.String("name", "noname", "give the node a name at runtime") tserver := flag.String("s", "localhost:50051", "The location (including port) of the remote gRPC server.") tnetwork := flag.String("n", "nonetwork", "The node network you are attempting to join.") + tpublicip := flag.String("ip4", "nopubip", "The node network you are attempting to join.") tnoauto := flag.Bool("na", false, "No auto mode. If true, netmclient will not be installed as a system service and you will have to retrieve updates manually via checkin command.") tnoforward := flag.Bool("nf", false, "No Forward mode. If true, netclient will not check for IP forwarding. This may break functionality") command := flag.String("c", "required", "The command to run") @@ -108,7 +109,7 @@ func main() { } fmt.Println("Beginning agent installation.") - err := functions.Install(*taccesskey, *tpassword, *tserver, *tnetwork, *tnoauto, *taccesstoken, *tname) + err := functions.Install(*taccesskey, *tpassword, *tserver, *tnetwork, *tnoauto, *taccesstoken, *tname, *tpublicip) if err != nil { fmt.Println("Error encountered while installing.") if !strings.Contains(err.Error(), "ALREADY_INSTALLED") { diff --git a/scripts/netmaker-install-local.sh b/scripts/netmaker-install-local.sh index a9257fa6..3a8952de 100755 --- a/scripts/netmaker-install-local.sh +++ b/scripts/netmaker-install-local.sh @@ -13,6 +13,7 @@ sleep 10 echo "Installing Netmaker API" mkdir -p /etc/netmaker/config/environments +mkdir -p /etc/netmaker/config/dnsconfig cp ../netmaker /etc/netmaker/netmaker chmod +x /etc/netmaker/netmaker @@ -37,7 +38,7 @@ mongoconn: opts: '/?authSource=admin' EOL -cat >/etc/netmaker/config/Corefile</etc/netmaker/config/dnsconfig/Corefile<