diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 4c393df..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "certs"] - path = certs - url = https://git.daplie.com/Daplie/localhost.daplie.me-certificates.git diff --git a/certs b/certs deleted file mode 160000 index ec246b2..0000000 --- a/certs +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ec246b228c87336896c9315e492bc87b3c638e41 diff --git a/instrumentation.go b/instrumentation.go deleted file mode 100755 index 506f955..0000000 --- a/instrumentation.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -// InstrumentationTable holds points to various structures making them available for instrumentation. -type InstrumentationTable struct { - connectionTable *ConnectionTable -} - -func newInstrumentationTable(connectionTable *ConnectionTable) *InstrumentationTable { - return &InstrumentationTable{ - connectionTable: connectionTable, - } -} diff --git a/logging/loggers.go b/logging/loggers.go deleted file mode 100644 index 4e8f23a..0000000 --- a/logging/loggers.go +++ /dev/null @@ -1,26 +0,0 @@ -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/listener_admin.go b/rvpn/admin/listener_admin.go similarity index 62% rename from listener_admin.go rename to rvpn/admin/listener_admin.go index e6b95c1..52d4d8e 100644 --- a/listener_admin.go +++ b/rvpn/admin/listener_admin.go @@ -1,19 +1,19 @@ -package main +package admin import ( "html/template" "net/http" ) -//launchAdminListener - starts up http listeners and handles various URI paths -func launchAdminListener() { - loginfo.Println("starting launchAdminListener", *argServerBinding) +//LaunchAdminListener - starts up http listeners and handles various URI paths +func LaunchAdminListener(serverBinding *string) (err error) { + loginfo.Println("starting launchAdminListener", *serverBinding) mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { switch url := r.URL.Path; url { case "/": - handleConnectionWebSocket(connectionTable, w, r, false) + //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) @@ -25,16 +25,15 @@ func launchAdminListener() { http.Error(w, "Not Found", 404) } - }) s := &http.Server{ - Addr: *argServerAdminBinding, + Addr: *serverBinding, Handler: mux, } - err := s.ListenAndServe() + err = s.ListenAndServe() if err != nil { - logfatal.Println("ListenAndServe: ", err) - panic(err) + loginfo.Println("ListenAndServe: ", err) } + return } diff --git a/rvpn/admin/setup.go b/rvpn/admin/setup.go new file mode 100644 index 0000000..a9b231c --- /dev/null +++ b/rvpn/admin/setup.go @@ -0,0 +1,17 @@ +package admin + +import ( + "log" + "os" +) + +var ( + loginfo *log.Logger + logdebug *log.Logger + logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile +) + +func init() { + loginfo = log.New(os.Stdout, "INFO: admin: ", logFlags) + logdebug = log.New(os.Stdout, "DEBUG: admin:", logFlags) +} diff --git a/listener_client.go b/rvpn/client/listener_client.go similarity index 53% rename from listener_client.go rename to rvpn/client/listener_client.go index b84dede..6b80334 100644 --- a/listener_client.go +++ b/rvpn/client/listener_client.go @@ -1,20 +1,23 @@ -package main +package client import ( "net/http" jwt "github.com/dgrijalva/jwt-go" + "github.com/gorilla/websocket" + + "git.daplie.com/Daplie/go-rvpn-server/rvpn/connection" ) -//launchClientListener - starts up http listeners and handles various URI paths -func launchClientListener() { - loginfo.Println("starting WebRequestExternal Listener ", *argServerBinding) +//LaunchClientListener - starts up http listeners and handles various URI paths +func LaunchClientListener(connectionTable *connection.Table, secretKey *string, serverBinding *string) (err error) { + loginfo.Println("starting WebRequestExternal Listener ", *serverBinding) mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { switch url := r.URL.Path; url { case "/": - handleConnectionWebSocket(connectionTable, w, r, false) + handleConnectionWebSocket(connectionTable, w, r, *secretKey, false) default: http.Error(w, "Not Found", 404) @@ -24,19 +27,19 @@ func launchClientListener() { }) s := &http.Server{ - Addr: *argServerBinding, + Addr: *serverBinding, Handler: mux, } - err := s.ListenAndServeTLS("certs/fullchain.pem", "certs/privkey.pem") + err = s.ListenAndServeTLS("certs/fullchain.pem", "certs/privkey.pem") if err != nil { - logfatal.Println("ListenAndServeTLS: ", err) - panic(err) + loginfo.Println("ListenAndServeTLS: ", err) } + return } // handleConnectionWebSocket handles websocket requests from the peer. -func handleConnectionWebSocket(connectionTable *ConnectionTable, w http.ResponseWriter, r *http.Request, admin bool) { +func handleConnectionWebSocket(connectionTable *connection.Table, w http.ResponseWriter, r *http.Request, secretKey string, admin bool) { loginfo.Println("websocket opening ", r.RemoteAddr, " ", r.Host) tokenString := r.URL.Query().Get("access_token") @@ -62,15 +65,22 @@ func handleConnectionWebSocket(connectionTable *ConnectionTable, w http.Response loginfo.Println("Recognized connection, waiting authentication") } + var upgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, + } + conn, err := upgrader.Upgrade(w, r, nil) if err != nil { loginfo.Println("WebSocket upgrade failed", err) return } loginfo.Println("before connection table") - connection := &Connection{connectionTable: connectionTable, conn: conn, send: make(chan []byte, 256), source: r.RemoteAddr, admin: admin} - connection.connectionTable.register <- connection - go connection.writer() - //go connection.sender() - connection.reader() + + //connection := &connection.Connection{connectionTable: connectionTable, conn: conn, send: make(chan []byte, 256), source: r.RemoteAddr, admin: admin} + + connection := connection.NewConnection(connectionTable, conn, r.RemoteAddr) + connectionTable.Register() <- connection + go connection.Writer() + connection.Reader() } diff --git a/rvpn/client/setup.go b/rvpn/client/setup.go new file mode 100644 index 0000000..5be0c5e --- /dev/null +++ b/rvpn/client/setup.go @@ -0,0 +1,17 @@ +package client + +import ( + "log" + "os" +) + +var ( + loginfo *log.Logger + logdebug *log.Logger + logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile +) + +func init() { + loginfo = log.New(os.Stdout, "INFO: client: ", logFlags) + logdebug = log.New(os.Stdout, "DEBUG: client:", logFlags) +} \ No newline at end of file diff --git a/connection.go b/rvpn/connection/connection.go similarity index 71% rename from connection.go rename to rvpn/connection/connection.go index 737da0b..4d1322d 100755 --- a/connection.go +++ b/rvpn/connection/connection.go @@ -1,4 +1,4 @@ -package main +package connection import ( "encoding/hex" @@ -15,7 +15,7 @@ var upgrader = websocket.Upgrader{ // Connection track websocket and faciliates in and out data type Connection struct { - connectionTable *ConnectionTable + connectionTable *Table // The websocket connection. conn *websocket.Conn @@ -26,9 +26,6 @@ type Connection struct { // Address of the Remote End Point source string - // admin flag. Grants access to admin features - admin bool - // bytes in bytesIn int64 @@ -36,6 +33,18 @@ type Connection struct { bytesOut int64 } +//NewConnection -- Constructor +func NewConnection(connectionTable *Table, conn *websocket.Conn, remoteAddress string) (p *Connection) { + p = new(Connection) + p.connectionTable = connectionTable + p.conn = conn + p.source = remoteAddress + p.bytesIn = 0 + p.bytesOut = 0 + p.send = make(chan []byte, 256) + return +} + func (c *Connection) addIn(num int64) { c.bytesIn = c.bytesIn + num } @@ -44,12 +53,19 @@ func (c *Connection) addOut(num int64) { c.bytesOut = c.bytesOut + num } -func (c *Connection) reader() { +//ConnectionTable -- property +func (c *Connection) ConnectionTable() (table *Table) { + table = c.connectionTable + return +} + +//Reader -- export the reader function +func (c *Connection) Reader() { defer func() { c.connectionTable.unregister <- c c.conn.Close() }() - c.conn.SetReadLimit(maxMessageSize) + c.conn.SetReadLimit(1024) for { _, message, err := c.conn.ReadMessage() if err != nil { @@ -64,7 +80,8 @@ func (c *Connection) reader() { } } -func (c *Connection) writer() { +//Writer -- expoer the writer function +func (c *Connection) Writer() { dwell := time.NewTicker(5 * time.Second) loginfo.Println("activate timer", dwell) defer func() { diff --git a/connection_table.go b/rvpn/connection/connection_table.go similarity index 70% rename from connection_table.go rename to rvpn/connection/connection_table.go index 6285aec..9a5a29c 100755 --- a/connection_table.go +++ b/rvpn/connection/connection_table.go @@ -1,21 +1,23 @@ -package main +package connection -// ConnectionTable maintains the set of connections -type ConnectionTable struct { +//Table maintains the set of connections +type Table struct { connections map[*Connection]bool register chan *Connection unregister chan *Connection } -func newConnectionTable() *ConnectionTable { - return &ConnectionTable{ +//NewTable -- consructor +func NewTable() *Table { + return &Table{ register: make(chan *Connection), unregister: make(chan *Connection), connections: make(map[*Connection]bool), } } -func (c *ConnectionTable) run() { +//Run -- Execute +func (c *Table) Run() { loginfo.Println("ConnectionTable starting") for { select { @@ -37,3 +39,9 @@ func (c *ConnectionTable) run() { } } } + +//Register -- Property +func (c *Table) Register() (r chan *Connection) { + r = c.register + return +} diff --git a/rvpn/connection/setup.go b/rvpn/connection/setup.go new file mode 100644 index 0000000..ed9d89a --- /dev/null +++ b/rvpn/connection/setup.go @@ -0,0 +1,17 @@ +package connection + +import ( + "log" + "os" +) + +var ( + loginfo *log.Logger + logdebug *log.Logger + logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile +) + +func init() { + loginfo = log.New(os.Stdout, "INFO: connection: ", logFlags) + logdebug = log.New(os.Stdout, "DEBUG: connection:", logFlags) +} diff --git a/listener_webrequest.go b/rvpn/external/listener_webrequest.go similarity index 52% rename from listener_webrequest.go rename to rvpn/external/listener_webrequest.go index b1acef3..3d130ea 100644 --- a/listener_webrequest.go +++ b/rvpn/external/listener_webrequest.go @@ -1,12 +1,16 @@ -package main +package external -import "net/http" -import "net/http/httputil" +import ( + "fmt" + "net" + "net/http" + "net/http/httputil" +) //launchWebRequestListener - starts up extern http listeners, gets request and prep's to hand it off inside. -func launchWebRequestExternalListener() { +func LaunchWebRequestExternalListener(serverBinding *string) { - loginfo.Println("starting WebRequestExternal Listener ", *argServerExternalBinding) + loginfo.Println("starting WebRequestExternal Listener ", *serverBinding) mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { @@ -25,13 +29,20 @@ func launchWebRequestExternalListener() { }) s := &http.Server{ - Addr: *argServerExternalBinding, - Handler: mux, + Addr: *serverBinding, + Handler: mux, + ConnState: connState, } err := s.ListenAndServe() if err != nil { - logfatal.Println("ListenAndServe: ", err) + loginfo.Println("ListenAndServe: ", err) panic(err) } } + +func connState(conn net.Conn, state http.ConnState) { + loginfo.Println("connState") + fmt.Println(conn, conn.LocalAddr(), conn.RemoteAddr()) + fmt.Println(state) +} diff --git a/rvpn/external/setup.go b/rvpn/external/setup.go new file mode 100644 index 0000000..1bff29b --- /dev/null +++ b/rvpn/external/setup.go @@ -0,0 +1,17 @@ +package external + +import ( + "log" + "os" +) + +var ( + loginfo *log.Logger + logdebug *log.Logger + logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile +) + +func init() { + loginfo = log.New(os.Stdout, "INFO: external: ", logFlags) + logdebug = log.New(os.Stdout, "DEBUG: external:", logFlags) +} \ No newline at end of file diff --git a/html/admin.html b/rvpn/html/admin.html similarity index 100% rename from html/admin.html rename to rvpn/html/admin.html diff --git a/html/client.html b/rvpn/html/client.html similarity index 100% rename from html/client.html rename to rvpn/html/client.html diff --git a/rvpn/instrumentation/setup.go b/rvpn/instrumentation/setup.go new file mode 100755 index 0000000..bec78c7 --- /dev/null +++ b/rvpn/instrumentation/setup.go @@ -0,0 +1,17 @@ +package instrumentation + +import ( + "log" + "os" +) + +var ( + loginfo *log.Logger + logdebug *log.Logger + logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile +) + +func init() { + loginfo = log.New(os.Stdout, "INFO: instrumentation: ", logFlags) + logdebug = log.New(os.Stdout, "DEBUG: instrumentation:", logFlags) +} \ No newline at end of file diff --git a/rvpn/main.go b/rvpn/main.go new file mode 100644 index 0000000..726300f --- /dev/null +++ b/rvpn/main.go @@ -0,0 +1,10 @@ +package main + +import "git.daplie.com/Daplie/go-rvpn-server/rvpn/rvpnmain" + +var run = rvpnmain.Run + +func main() { + run() + +} diff --git a/rvpn/packer/packer.go b/rvpn/packer/packer.go new file mode 100644 index 0000000..bba27e6 --- /dev/null +++ b/rvpn/packer/packer.go @@ -0,0 +1,15 @@ +package packer + +//Packer -- contains both header and data +type Packer struct { + Header *packerHeader + Data *packerData +} + +//NewPacker -- Structre +func NewPacker() (p *Packer) { + p = new(Packer) + p.Header = newPackerHeader() + p.Data = newPackerData() + return +} diff --git a/rvpn/packer/packer_data.go b/rvpn/packer/packer_data.go new file mode 100644 index 0000000..03eda91 --- /dev/null +++ b/rvpn/packer/packer_data.go @@ -0,0 +1,14 @@ +package packer + +import "bytes" + +//packerData -- Contains packer data +type packerData struct { + Buffer *bytes.Buffer +} + +func newPackerData() (p *packerData) { + p = new(packerData) + p.Buffer = new(bytes.Buffer) + return +} diff --git a/rvpn/packer/packer_header.go b/rvpn/packer/packer_header.go new file mode 100644 index 0000000..3b29086 --- /dev/null +++ b/rvpn/packer/packer_header.go @@ -0,0 +1,56 @@ +package packer + +import "net" +import "fmt" + +// packerHeader structure to hold our header information. +type packerHeader struct { + family addressFamily + address net.IP + Port int + Service string +} + +type addressFamily int +type addressFamilyString string + +//Family -- ENUM for Address Family +const ( + FamilyIPv4 addressFamily = iota + FamilyIPv6 +) + +func newPackerHeader() (p *packerHeader) { + p = new(packerHeader) + p.SetAddress("127.0.0.1") + p.Port = 65535 + p.Service = "na" + return +} + +//SetAddress -- Set Address. which sets address family automatically +func (p *packerHeader) SetAddress(addr string) { + p.address = net.ParseIP(addr) + err := p.address.To4() + + if err != nil { + p.family = FamilyIPv4 + } else { + err := p.address.To16() + if err != nil { + p.family = FamilyIPv6 + } else { + panic(fmt.Sprintf("setAddress does not support %s", addr)) + } + } +} + +func (p *packerHeader) Address() (address net.IP) { + address = p.address + return +} + +func (p *packerHeader) Family() (family addressFamily) { + family = p.family + return +} diff --git a/rvpn/packer/setup.go b/rvpn/packer/setup.go new file mode 100644 index 0000000..33dd55e --- /dev/null +++ b/rvpn/packer/setup.go @@ -0,0 +1,13 @@ +package packer + +import "log" +import "os" + +func init() { + logFlags := log.Ldate | log.Lmicroseconds | log.Lshortfile + loginfo := log.New(os.Stdout, "INFO: packer: ", logFlags) + logdebug := log.New(os.Stdout, "DEBUG: packer:", logFlags) + + loginfo.Println("") + logdebug.Println("") +} diff --git a/rvpn/rvpn b/rvpn/rvpn new file mode 100755 index 0000000..64d3de4 Binary files /dev/null and b/rvpn/rvpn differ diff --git a/rvpn/rvpnmain/run.go b/rvpn/rvpnmain/run.go new file mode 100644 index 0000000..648b685 --- /dev/null +++ b/rvpn/rvpnmain/run.go @@ -0,0 +1,65 @@ +package rvpnmain + +import ( + "flag" + "fmt" + "log" + "os" + + "git.daplie.com/Daplie/go-rvpn-server/rvpn/admin" + "git.daplie.com/Daplie/go-rvpn-server/rvpn/client" + "git.daplie.com/Daplie/go-rvpn-server/rvpn/connection" + "git.daplie.com/Daplie/go-rvpn-server/rvpn/external" + "git.daplie.com/Daplie/go-rvpn-server/rvpn/packer" +) + +var ( + loginfo *log.Logger + logdebug *log.Logger + logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile + argServerBinding string + argServerAdminBinding string + argServerExternalBinding string + connectionTable *connection.Table + secretKey = "abc123" +) + +func init() { + flag.StringVar(&argServerBinding, "server-port", "127.0.0.1:3502", "server Bind listener") + flag.StringVar(&argServerAdminBinding, "admin-server-port", "127.0.0.2:8000", "admin server Bind listener") + flag.StringVar(&argServerExternalBinding, "external-server-port", "127.0.0.1:8080", "external server Bind listener") + +} + +//Run -- main entry point +func Run() { + flag.Parse() + + loginfo = log.New(os.Stdout, "INFO: packer: ", logFlags) + logdebug = log.New(os.Stdout, "DEBUG: packer:", logFlags) + + loginfo.Println("startup") + + p := packer.NewPacker() + fmt.Println(*p.Header) + + p.Header.SetAddress("127.0.0.2") + fmt.Println(*p.Header) + + p.Header.SetAddress("2001:db8::1") + fmt.Println(*p.Header) + + fmt.Println(p.Header.Address()) + + loginfo.Println(p) + + connectionTable = connection.NewTable() + go connectionTable.Run() + go client.LaunchClientListener(connectionTable, &secretKey, &argServerBinding) + go external.LaunchWebRequestExternalListener(&argServerExternalBinding) + + err := admin.LaunchAdminListener(&argServerAdminBinding) + if err != nil { + loginfo.Println("LauchAdminListener failed: ", err) + } +} diff --git a/vpn-server.go b/vpn-server.go deleted file mode 100644 index 8a697aa..0000000 --- a/vpn-server.go +++ /dev/null @@ -1,58 +0,0 @@ -package main - -import ( - "flag" - "io" - "log" - "os" - "time" - - "git.daplie.com/Daplie/go-rvpn-server/logging" -) - -const ( - // Time allowed to write a message to the peer. - writeWait = 10 * time.Second - - // Time allowed to read the next pong message from the peer. - pongWait = 60 * time.Second - - // Send pings to peer with this period. Must be less than pongWait. - pingPeriod = (pongWait * 9) / 10 - - // Maximum message size allowed from peer. - maxMessageSize = 512 -) - -var ( - //Info .. - loginfo *log.Logger - logfatal *log.Logger - logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile - argServerBinding = flag.String("server-port", "127.0.0.1:3502", "server Bind listener") - argServerAdminBinding = flag.String("admin-server-port", "127.0.0.2:8000", "admin server Bind listener") - argServerExternalBinding = flag.String("external-server-port", "127.0.0.1:8080", "external server Bind listener") - connectionTable *ConnectionTable - secretKey = "abc123" -) - -func logInit(infoHandle io.Writer) { - loginfo = log.New(infoHandle, "INFO: ", logFlags) - logfatal = log.New(infoHandle, "FATAL : ", logFlags) -} - -func main() { - logging.Init(os.Stdout, logFlags) - linfo, lfatal := logging.Get() - loginfo = linfo - logfatal = lfatal - - loginfo.Println("startup") - flag.Parse() - - connectionTable = newConnectionTable() - go connectionTable.run() - go launchClientListener() - go launchWebRequestExternalListener() - launchAdminListener() -}