Browse Source

server WIP

Min 6 years ago
parent
commit
5ca157cd2c
4 changed files with 105 additions and 4 deletions
  1. 46 0
      server/config.go
  2. 52 0
      server/inteface.go
  3. 2 0
      server/main.go
  4. 5 4
      server/server.go

+ 46 - 0
server/config.go

@@ -0,0 +1,46 @@
+package main
+
+import (
+	"encoding/json"
+	"io/ioutil"
+	"os"
+)
+
+var configFile = "config.json"
+var config = &configMain{
+	Admins: map[string]configAdmin{},
+	Boards: map[string]configBoard{},
+}
+
+type configMain struct{
+	Admins map[string]configAdmin `json:"admins"`
+	Boards map[string]configBoard `json:"boards"`
+}
+
+type configAdmin struct{
+	Username string `json:"user"`
+	Hash []byte		`json:"hash"`
+}
+
+type configBoard struct{
+	ID uint16	`json:"ID"`
+	KEY []byte	`json:"KEY"`
+}
+
+func loadConfig() {
+	if _, err := os.Stat(configFile); os.IsNotExist(err) {
+		return
+	}
+	jsonFile, err := os.Open(configFile)
+	errCheckExit(err, "Unable to open config file")
+	defer jsonFile.Close()
+	byteValue, _ := ioutil.ReadAll(jsonFile)
+	err = json.Unmarshal(byteValue, config)
+	errCheck(err, "Failed to unmarshal config")
+}
+
+func saveConfig() {
+	jsonValue, _ := json.Marshal(config)
+	err := ioutil.WriteFile(configFile, jsonValue, 0644)
+	errCheck(err, "Failed to write config")
+}

+ 52 - 0
server/inteface.go

@@ -0,0 +1,52 @@
+package main
+
+import (
+	"fmt"
+	"github.com/labstack/echo"
+	"github.com/labstack/echo/middleware"
+	"golang.org/x/crypto/bcrypt"
+	"net/http"
+)
+
+func serveInterface(address string) {
+	e := echo.New()
+	e.GET("/", func(c echo.Context) error {
+		return c.String(http.StatusOK, "ELEC0017 Project")
+		//return c.JSON(http.StatusOK, &config)
+	})
+	e.GET("/new/admin", func(c echo.Context) error {
+		name := c.QueryParam("username")
+		pass := c.QueryParam("password")
+		if len(name) == 0 {
+			return c.String(400, "No username")
+		}
+		if len(pass) == 0 {
+			return c.String(400, "No password")
+		}
+		hash, err := bcrypt.GenerateFromPassword([]byte(pass), 14)
+		if err != nil {
+			fmt.Printf("Failed hash password %s\n", err)
+			return c.String(400, "Error")
+		}
+		cx := *config
+		cx.Admins[name] = configAdmin{name, hash}
+		saveConfig()
+		return c.String(200, "OK")
+	})
+
+	//g := e.Group("/admin")
+	e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
+		user, ok := config.Admins[username]
+		if !ok {
+			return false, nil
+		}
+		err := bcrypt.CompareHashAndPassword(user.Hash, []byte(password))
+		return err == nil, nil
+	}))
+	e.GET("/admin/config", func(c echo.Context) error {
+		return c.JSON(http.StatusOK, &config)
+	})
+
+	e.Logger.Fatal(e.Start(address))
+
+}

+ 2 - 0
server/main.go

@@ -1,5 +1,7 @@
 package main
 
 func main() {
+	loadConfig()
+	go serveInterface(":9080")
 	serve(":9000")
 }

+ 5 - 4
server/server.go

@@ -11,6 +11,7 @@ import (
 
 var aes_key = "03020100070605040b0a09080f0e0d0c"
 
+
 func serveConnection(conn net.Conn) {
 	defer logPanic()
 	defer conn.Close()
@@ -28,11 +29,11 @@ func serveConnection(conn net.Conn) {
 		errCheckPanic(err, "Connection failed %s device #%d", conn.RemoteAddr().String(), id)
 		fmt.Printf("Payload: %x\n", payload)
 		message := make([]byte, msglen-16)
-		c, err := aes.NewCipher(decoded)
+		block, err := aes.NewCipher(decoded)
 		errCheckPanic(err, "Decryption failed %s device #%d", conn.RemoteAddr().String(), id)
-		cbc := cipher.NewCBCEncrypter(c, payload[:16])
-		cbc.CryptBlocks(payload[16:], message)
-		fmt.Printf("Message: %x\n", message)
+		mode := cipher.NewCBCDecrypter(block, payload[:16])
+		mode.CryptBlocks(message, payload[16:])
+		fmt.Printf("Message: %s\n", string(message))
 	}
 
 }