Updated, restored file and tested.

- certs and traffic counts are working
This commit is contained in:
Henry Camacho 2017-02-02 17:25:12 -06:00
parent b29a43f7a8
commit d3747c809a
1 changed files with 92 additions and 0 deletions

92
vpn-server.go Normal file
View File

@ -0,0 +1,92 @@
package main
import (
"flag"
"html/template"
"io"
"log"
"net/http"
"os"
"time"
)
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.Ltime | log.Lshortfile
argServerPort = flag.String("server-port", ":8000", "serverPort listener")
connectionTable *ConnectionTable
)
func logInit(infoHandle io.Writer) {
loginfo = log.New(infoHandle, "INFO: ", 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 "/":
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)
loginfo.Println("startup")
flag.Parse()
loginfo.Println(*argServerPort)
go launchListener()
time.Sleep(600 * time.Second)
}