Restructured code, using module use, created logging package as a helper
- altered code to support a client bound interface - altered code to support an admin bound interface - added configurations and defaults. - removed timeout function at the end of main. - the final go routine was converted to a foreground routine.
This commit is contained in:
parent
7e3c6e061a
commit
894bd997a9
|
@ -1,3 +1,5 @@
|
||||||
/go-rvpn-server
|
/go-rvpn-server
|
||||||
/m
|
/m
|
||||||
|
/debug
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"time"
|
"time"
|
||||||
|
@ -57,13 +56,12 @@ func (c *Connection) reader() {
|
||||||
_, message, err := c.conn.ReadMessage()
|
_, message, err := c.conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
||||||
log.Printf("error: %v", err)
|
loginfo.Printf("error: %v", err)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
loginfo.Println(hex.Dump(message))
|
loginfo.Println(hex.Dump(message))
|
||||||
c.addIn(int64(len(message)))
|
c.addIn(int64(len(message)))
|
||||||
|
|
||||||
loginfo.Println(c)
|
loginfo.Println(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,7 +108,7 @@ func (c *Connection) sender() {
|
||||||
|
|
||||||
// handleConnectionWebSocket handles websocket requests from the peer.
|
// handleConnectionWebSocket handles websocket requests from the peer.
|
||||||
func handleConnectionWebSocket(connectionTable *ConnectionTable, w http.ResponseWriter, r *http.Request, admin bool) {
|
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")
|
tokenString := r.URL.Query().Get("access_token")
|
||||||
result, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
result, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
|
|
@ -28,9 +28,11 @@ func (c *ConnectionTable) run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
case connection := <-c.unregister:
|
case connection := <-c.unregister:
|
||||||
|
loginfo.Println("closing connection ", connection)
|
||||||
if _, ok := c.connections[connection]; ok {
|
if _, ok := c.connections[connection]; ok {
|
||||||
delete(c.connections, connection)
|
delete(c.connections, connection)
|
||||||
close(connection.send)
|
close(connection.send)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
|
@ -2,12 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"html/template"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.daplie.com/Daplie/go-rvpn-server/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -26,12 +26,13 @@ const (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
//Info ..
|
//Info ..
|
||||||
loginfo *log.Logger
|
loginfo *log.Logger
|
||||||
logfatal *log.Logger
|
logfatal *log.Logger
|
||||||
logFlags = log.Ldate | log.Ltime | log.Lshortfile
|
logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
|
||||||
argServerPort = flag.String("server-port", ":8000", "serverPort listener")
|
argServerBinding = flag.String("server-port", "127.0.0.1:8000", "server Bind listener")
|
||||||
connectionTable *ConnectionTable
|
argServerAdminBinding = flag.String("admin-server-port", "127.0.0.2:8000", "admin server Bind listener")
|
||||||
secretKey = "abc123"
|
connectionTable *ConnectionTable
|
||||||
|
secretKey = "abc123"
|
||||||
)
|
)
|
||||||
|
|
||||||
func logInit(infoHandle io.Writer) {
|
func logInit(infoHandle io.Writer) {
|
||||||
|
@ -39,56 +40,15 @@ func logInit(infoHandle io.Writer) {
|
||||||
logfatal = log.New(infoHandle, "FATAL : ", logFlags)
|
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() {
|
func main() {
|
||||||
logInit(os.Stdout)
|
logging.Init(os.Stdout, logFlags)
|
||||||
|
linfo, lfatal := logging.Get()
|
||||||
|
loginfo = linfo
|
||||||
|
logfatal = lfatal
|
||||||
|
|
||||||
loginfo.Println("startup")
|
loginfo.Println("startup")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
loginfo.Println(*argServerPort)
|
|
||||||
|
|
||||||
go launchListener()
|
go launchClientListener()
|
||||||
time.Sleep(600 * time.Second)
|
launchAdminListener()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue