diff --git a/.gitignore b/.gitignore index 5969a51..6b45014 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /go-rvpn-server /m +/debug + diff --git a/connection.go b/connection.go index d39db8f..3be442a 100755 --- a/connection.go +++ b/connection.go @@ -2,7 +2,6 @@ package main import ( "encoding/hex" - "log" "net/http" "time" @@ -57,13 +56,12 @@ func (c *Connection) reader() { _, message, err := c.conn.ReadMessage() if err != nil { if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) { - log.Printf("error: %v", err) + loginfo.Printf("error: %v", err) } break } loginfo.Println(hex.Dump(message)) c.addIn(int64(len(message))) - loginfo.Println(c) } } @@ -110,7 +108,7 @@ func (c *Connection) sender() { // handleConnectionWebSocket handles websocket requests from the peer. func handleConnectionWebSocket(connectionTable *ConnectionTable, w http.ResponseWriter, r *http.Request, admin bool) { - loginfo.Println("websocket opening ", r.RemoteAddr) + loginfo.Println("websocket opening ", r.RemoteAddr, " ", r.Host) tokenString := r.URL.Query().Get("access_token") result, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { diff --git a/connection_table.go b/connection_table.go index cfa4e81..6285aec 100755 --- a/connection_table.go +++ b/connection_table.go @@ -28,9 +28,11 @@ func (c *ConnectionTable) run() { } case connection := <-c.unregister: + loginfo.Println("closing connection ", connection) if _, ok := c.connections[connection]; ok { delete(c.connections, connection) close(connection.send) + } } } diff --git a/listener_admin.go b/listener_admin.go new file mode 100644 index 0000000..517346f --- /dev/null +++ b/listener_admin.go @@ -0,0 +1,36 @@ +package main + +import ( + "html/template" + "net/http" +) + +//launchAdminListener - starts up http listeners and handles various URI paths +func launchAdminListener() { + loginfo.Println("starting Admin Listener") + + http.HandleFunc("/admin", handlerServeAdminContent) + + err := http.ListenAndServeTLS(*argServerAdminBinding, "certs/fullchain.pem", "certs/privkey.pem", nil) + if err != nil { + logfatal.Println("ListenAndServe: ", err) + panic(err) + } +} + +func handlerServeAdminContent(w http.ResponseWriter, r *http.Request) { + switch url := r.URL.Path; url { + case "/": + handleConnectionWebSocket(connectionTable, w, r, false) + //w.Header().Set("Content-Type", "text/html; charset=utf-8") + //template.Must(template.ParseFiles("html/client.html")).Execute(w, r.Host) + + case "/admin": + w.Header().Set("Content-Type", "text/html; charset=utf-8") + template.Must(template.ParseFiles("html/admin.html")).Execute(w, r.Host) + + default: + http.Error(w, "Not Found", 404) + + } +} diff --git a/listener_client.go b/listener_client.go new file mode 100644 index 0000000..4d18f96 --- /dev/null +++ b/listener_client.go @@ -0,0 +1,44 @@ +package main + +import ( + "html/template" + "net/http" +) + +//launchListener - starts up http listeners and handles various URI paths +func launchClientListener() { + loginfo.Println("starting Client Listener ", argServerBinding) + + connectionTable = newConnectionTable() + go connectionTable.run() + http.HandleFunc("/", handlerServeContent) + + err := http.ListenAndServeTLS(*argServerBinding, "certs/fullchain.pem", "certs/privkey.pem", nil) + if err != nil { + logfatal.Println("ListenAndServe: ", err) + panic(err) + } +} + +func handlerServeContent(w http.ResponseWriter, r *http.Request) { + switch url := r.URL.Path; url { + case "/": + handleConnectionWebSocket(connectionTable, w, r, false) + //w.Header().Set("Content-Type", "text/html; charset=utf-8") + //template.Must(template.ParseFiles("html/client.html")).Execute(w, r.Host) + + case "/admin": + w.Header().Set("Content-Type", "text/html; charset=utf-8") + template.Must(template.ParseFiles("html/admin.html")).Execute(w, r.Host) + + case "/ws/client": + handleConnectionWebSocket(connectionTable, w, r, false) + + case "/ws/admin": + handleConnectionWebSocket(connectionTable, w, r, true) + + default: + http.Error(w, "Not Found", 404) + + } +} diff --git a/logging/loggers.go b/logging/loggers.go new file mode 100644 index 0000000..4e8f23a --- /dev/null +++ b/logging/loggers.go @@ -0,0 +1,26 @@ +package logging + +import ( + "io" + "log" +) + +// Logging structure used for setup of logging +var ( + logflags int + loginfo *log.Logger + logfatal *log.Logger +) + +// Init configure logging structures +func Init(writer io.Writer, flags int) { + loginfo = log.New(writer, "INFO: ", flags) + logfatal = log.New(writer, "INFO: ", flags) +} + +// Get loggingers +func Get() (linfo *log.Logger, lfatal *log.Logger) { + linfo = loginfo + lfatal = logfatal + return +} diff --git a/vpn-server.go b/vpn-server.go index 3dd874b..8922513 100644 --- a/vpn-server.go +++ b/vpn-server.go @@ -2,12 +2,12 @@ package main import ( "flag" - "html/template" "io" "log" - "net/http" "os" "time" + + "git.daplie.com/Daplie/go-rvpn-server/logging" ) const ( @@ -26,12 +26,13 @@ const ( var ( //Info .. - loginfo *log.Logger - logfatal *log.Logger - logFlags = log.Ldate | log.Ltime | log.Lshortfile - argServerPort = flag.String("server-port", ":8000", "serverPort listener") - connectionTable *ConnectionTable - secretKey = "abc123" + loginfo *log.Logger + logfatal *log.Logger + logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile + argServerBinding = flag.String("server-port", "127.0.0.1:8000", "server Bind listener") + argServerAdminBinding = flag.String("admin-server-port", "127.0.0.2:8000", "admin server Bind listener") + connectionTable *ConnectionTable + secretKey = "abc123" ) func logInit(infoHandle io.Writer) { @@ -39,56 +40,15 @@ func logInit(infoHandle io.Writer) { logfatal = log.New(infoHandle, "FATAL : ", logFlags) } -/* -handlerServeContent -- Handles generic URI paths / -"/" - normal client activities for websocket, marked admin=false -"/admin" - marks incoming connection as admin, however must authenticate -"/ws/client" & "/ws/admin" websocket terminations -*/ -func handlerServeContent(w http.ResponseWriter, r *http.Request) { - switch url := r.URL.Path; url { - case "/": - handleConnectionWebSocket(connectionTable, w, r, false) - //w.Header().Set("Content-Type", "text/html; charset=utf-8") - //template.Must(template.ParseFiles("html/client.html")).Execute(w, r.Host) - - case "/admin": - w.Header().Set("Content-Type", "text/html; charset=utf-8") - template.Must(template.ParseFiles("html/admin.html")).Execute(w, r.Host) - - case "/ws/client": - handleConnectionWebSocket(connectionTable, w, r, false) - - case "/ws/admin": - handleConnectionWebSocket(connectionTable, w, r, true) - - default: - http.Error(w, "Not Found", 404) - - } -} - -//launchListener - starts up http listeners and handles various URI paths -func launchListener() { - loginfo.Println("starting Listener") - - connectionTable = newConnectionTable() - go connectionTable.run() - http.HandleFunc("/", handlerServeContent) - - err := http.ListenAndServeTLS(*argServerPort, "certs/fullchain.pem", "certs/privkey.pem", nil) - if err != nil { - logfatal.Println("ListenAndServe: ", err) - panic(err) - } -} - func main() { - logInit(os.Stdout) + logging.Init(os.Stdout, logFlags) + linfo, lfatal := logging.Get() + loginfo = linfo + logfatal = lfatal + loginfo.Println("startup") flag.Parse() - loginfo.Println(*argServerPort) - go launchListener() - time.Sleep(600 * time.Second) + go launchClientListener() + launchAdminListener() }