Restructured project abstracting modules, using Caddy as a guide.

This commit is contained in:
Henry Camacho 2017-02-11 22:13:29 -06:00
parent 6d172c2404
commit a5450dd205
24 changed files with 350 additions and 147 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "certs"]
path = certs
url = https://git.daplie.com/Daplie/localhost.daplie.me-certificates.git

1
certs

@ -1 +0,0 @@
Subproject commit ec246b228c87336896c9315e492bc87b3c638e41

View File

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

View File

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

View File

@ -1,19 +1,19 @@
package main package admin
import ( import (
"html/template" "html/template"
"net/http" "net/http"
) )
//launchAdminListener - starts up http listeners and handles various URI paths //LaunchAdminListener - starts up http listeners and handles various URI paths
func launchAdminListener() { func LaunchAdminListener(serverBinding *string) (err error) {
loginfo.Println("starting launchAdminListener", *argServerBinding) loginfo.Println("starting launchAdminListener", *serverBinding)
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch url := r.URL.Path; url { switch url := r.URL.Path; url {
case "/": case "/":
handleConnectionWebSocket(connectionTable, w, r, false) //handleConnectionWebSocket(connectionTable, w, r, false)
//w.Header().Set("Content-Type", "text/html; charset=utf-8") //w.Header().Set("Content-Type", "text/html; charset=utf-8")
//template.Must(template.ParseFiles("html/client.html")).Execute(w, r.Host) //template.Must(template.ParseFiles("html/client.html")).Execute(w, r.Host)
@ -25,16 +25,15 @@ func launchAdminListener() {
http.Error(w, "Not Found", 404) http.Error(w, "Not Found", 404)
} }
}) })
s := &http.Server{ s := &http.Server{
Addr: *argServerAdminBinding, Addr: *serverBinding,
Handler: mux, Handler: mux,
} }
err := s.ListenAndServe() err = s.ListenAndServe()
if err != nil { if err != nil {
logfatal.Println("ListenAndServe: ", err) loginfo.Println("ListenAndServe: ", err)
panic(err)
} }
return
} }

17
rvpn/admin/setup.go Normal file
View File

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

View File

@ -1,20 +1,23 @@
package main package client
import ( import (
"net/http" "net/http"
jwt "github.com/dgrijalva/jwt-go" 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 //LaunchClientListener - starts up http listeners and handles various URI paths
func launchClientListener() { func LaunchClientListener(connectionTable *connection.Table, secretKey *string, serverBinding *string) (err error) {
loginfo.Println("starting WebRequestExternal Listener ", *argServerBinding) loginfo.Println("starting WebRequestExternal Listener ", *serverBinding)
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch url := r.URL.Path; url { switch url := r.URL.Path; url {
case "/": case "/":
handleConnectionWebSocket(connectionTable, w, r, false) handleConnectionWebSocket(connectionTable, w, r, *secretKey, false)
default: default:
http.Error(w, "Not Found", 404) http.Error(w, "Not Found", 404)
@ -24,19 +27,19 @@ func launchClientListener() {
}) })
s := &http.Server{ s := &http.Server{
Addr: *argServerBinding, Addr: *serverBinding,
Handler: mux, Handler: mux,
} }
err := s.ListenAndServeTLS("certs/fullchain.pem", "certs/privkey.pem") err = s.ListenAndServeTLS("certs/fullchain.pem", "certs/privkey.pem")
if err != nil { if err != nil {
logfatal.Println("ListenAndServeTLS: ", err) loginfo.Println("ListenAndServeTLS: ", err)
panic(err)
} }
return
} }
// 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 *connection.Table, w http.ResponseWriter, r *http.Request, secretKey string, admin bool) {
loginfo.Println("websocket opening ", r.RemoteAddr, " ", r.Host) loginfo.Println("websocket opening ", r.RemoteAddr, " ", r.Host)
tokenString := r.URL.Query().Get("access_token") tokenString := r.URL.Query().Get("access_token")
@ -62,15 +65,22 @@ func handleConnectionWebSocket(connectionTable *ConnectionTable, w http.Response
loginfo.Println("Recognized connection, waiting authentication") loginfo.Println("Recognized connection, waiting authentication")
} }
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
conn, err := upgrader.Upgrade(w, r, nil) conn, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
loginfo.Println("WebSocket upgrade failed", err) loginfo.Println("WebSocket upgrade failed", err)
return return
} }
loginfo.Println("before connection table") 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 //connection := &connection.Connection{connectionTable: connectionTable, conn: conn, send: make(chan []byte, 256), source: r.RemoteAddr, admin: admin}
go connection.writer()
//go connection.sender() connection := connection.NewConnection(connectionTable, conn, r.RemoteAddr)
connection.reader() connectionTable.Register() <- connection
go connection.Writer()
connection.Reader()
} }

17
rvpn/client/setup.go Normal file
View File

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

View File

@ -1,4 +1,4 @@
package main package connection
import ( import (
"encoding/hex" "encoding/hex"
@ -15,7 +15,7 @@ var upgrader = websocket.Upgrader{
// Connection track websocket and faciliates in and out data // Connection track websocket and faciliates in and out data
type Connection struct { type Connection struct {
connectionTable *ConnectionTable connectionTable *Table
// The websocket connection. // The websocket connection.
conn *websocket.Conn conn *websocket.Conn
@ -26,9 +26,6 @@ type Connection struct {
// Address of the Remote End Point // Address of the Remote End Point
source string source string
// admin flag. Grants access to admin features
admin bool
// bytes in // bytes in
bytesIn int64 bytesIn int64
@ -36,6 +33,18 @@ type Connection struct {
bytesOut int64 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) { func (c *Connection) addIn(num int64) {
c.bytesIn = c.bytesIn + num c.bytesIn = c.bytesIn + num
} }
@ -44,12 +53,19 @@ func (c *Connection) addOut(num int64) {
c.bytesOut = c.bytesOut + num 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() { defer func() {
c.connectionTable.unregister <- c c.connectionTable.unregister <- c
c.conn.Close() c.conn.Close()
}() }()
c.conn.SetReadLimit(maxMessageSize) c.conn.SetReadLimit(1024)
for { for {
_, message, err := c.conn.ReadMessage() _, message, err := c.conn.ReadMessage()
if err != nil { 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) dwell := time.NewTicker(5 * time.Second)
loginfo.Println("activate timer", dwell) loginfo.Println("activate timer", dwell)
defer func() { defer func() {

View File

@ -1,21 +1,23 @@
package main package connection
// ConnectionTable maintains the set of connections //Table maintains the set of connections
type ConnectionTable struct { type Table struct {
connections map[*Connection]bool connections map[*Connection]bool
register chan *Connection register chan *Connection
unregister chan *Connection unregister chan *Connection
} }
func newConnectionTable() *ConnectionTable { //NewTable -- consructor
return &ConnectionTable{ func NewTable() *Table {
return &Table{
register: make(chan *Connection), register: make(chan *Connection),
unregister: make(chan *Connection), unregister: make(chan *Connection),
connections: make(map[*Connection]bool), connections: make(map[*Connection]bool),
} }
} }
func (c *ConnectionTable) run() { //Run -- Execute
func (c *Table) Run() {
loginfo.Println("ConnectionTable starting") loginfo.Println("ConnectionTable starting")
for { for {
select { select {
@ -37,3 +39,9 @@ func (c *ConnectionTable) run() {
} }
} }
} }
//Register -- Property
func (c *Table) Register() (r chan *Connection) {
r = c.register
return
}

17
rvpn/connection/setup.go Normal file
View File

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

View File

@ -1,12 +1,16 @@
package main package external
import "net/http" import (
import "net/http/httputil" "fmt"
"net"
"net/http"
"net/http/httputil"
)
//launchWebRequestListener - starts up extern http listeners, gets request and prep's to hand it off inside. //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 := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@ -25,13 +29,20 @@ func launchWebRequestExternalListener() {
}) })
s := &http.Server{ s := &http.Server{
Addr: *argServerExternalBinding, Addr: *serverBinding,
Handler: mux, Handler: mux,
ConnState: connState,
} }
err := s.ListenAndServe() err := s.ListenAndServe()
if err != nil { if err != nil {
logfatal.Println("ListenAndServe: ", err) loginfo.Println("ListenAndServe: ", err)
panic(err) panic(err)
} }
} }
func connState(conn net.Conn, state http.ConnState) {
loginfo.Println("connState")
fmt.Println(conn, conn.LocalAddr(), conn.RemoteAddr())
fmt.Println(state)
}

17
rvpn/external/setup.go vendored Normal file
View File

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

17
rvpn/instrumentation/setup.go Executable file
View File

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

10
rvpn/main.go Normal file
View File

@ -0,0 +1,10 @@
package main
import "git.daplie.com/Daplie/go-rvpn-server/rvpn/rvpnmain"
var run = rvpnmain.Run
func main() {
run()
}

15
rvpn/packer/packer.go Normal file
View File

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

View File

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

View File

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

13
rvpn/packer/setup.go Normal file
View File

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

BIN
rvpn/rvpn Executable file

Binary file not shown.

65
rvpn/rvpnmain/run.go Normal file
View File

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

View File

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