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:
Henry Camacho 2017-02-05 21:19:04 -06:00
parent 7e3c6e061a
commit 894bd997a9
7 changed files with 128 additions and 60 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
/go-rvpn-server
/m
/debug

View File

@ -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) {

View File

@ -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)
}
}
}

36
listener_admin.go Normal file
View File

@ -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)
}
}

44
listener_client.go Normal file
View File

@ -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)
}
}

26
logging/loggers.go Normal file
View File

@ -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
}

View File

@ -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()
}