mirror of
https://github.com/shlldev/miniws.git
synced 2025-09-02 19:00:59 +02:00
first ipc implementation
This commit is contained in:
8
go.mod
8
go.mod
@@ -1,11 +1,5 @@
|
|||||||
module github.com/shlldev/miniws
|
module main
|
||||||
|
|
||||||
go 1.24.5
|
go 1.24.5
|
||||||
|
|
||||||
require github.com/akamensky/argparse v1.4.0
|
require github.com/akamensky/argparse v1.4.0
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
|
||||||
github.com/wneessen/go-fileperm v0.2.1 // indirect
|
|
||||||
golang.org/x/sys v0.35.0 // indirect
|
|
||||||
)
|
|
||||||
|
2
go.work
2
go.work
@@ -3,4 +3,6 @@ go 1.24.5
|
|||||||
use (
|
use (
|
||||||
.
|
.
|
||||||
./miniws
|
./miniws
|
||||||
|
./miniws/sockets
|
||||||
|
./miniws/sockets/logplus
|
||||||
)
|
)
|
13
main.go
13
main.go
@@ -3,9 +3,11 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"sockets"
|
||||||
|
|
||||||
|
"miniws"
|
||||||
|
|
||||||
"github.com/akamensky/argparse"
|
"github.com/akamensky/argparse"
|
||||||
"github.com/shlldev/miniws/miniws"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -14,11 +16,13 @@ const (
|
|||||||
HELP_CONFIGFOLDER string = "the configurations folder"
|
HELP_CONFIGFOLDER string = "the configurations folder"
|
||||||
HELP_WWWFOLDER string = "the www folder where miniws will look for files to serve"
|
HELP_WWWFOLDER string = "the www folder where miniws will look for files to serve"
|
||||||
HELP_MAXLOGBYTES string = "the maximum bytes after which the log files get split"
|
HELP_MAXLOGBYTES string = "the maximum bytes after which the log files get split"
|
||||||
|
HELP_SIGNAL string = "runs the executable in command mode, meaning it will just send a command to the already running miniws server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
parser := argparse.NewParser("miniws", "")
|
parser := argparse.NewParser("miniws", "")
|
||||||
|
|
||||||
|
signal := parser.String("s", "signal", &argparse.Options{Default: "", Help: HELP_SIGNAL})
|
||||||
port := parser.Int("p", "port", &argparse.Options{Default: 8040, Help: HELP_PORT})
|
port := parser.Int("p", "port", &argparse.Options{Default: 8040, Help: HELP_PORT})
|
||||||
logFolder := parser.String("l", "logs-folder", &argparse.Options{Default: "logs", Help: HELP_LOGFOLDER})
|
logFolder := parser.String("l", "logs-folder", &argparse.Options{Default: "logs", Help: HELP_LOGFOLDER})
|
||||||
configFolder := parser.String("c", "config-folder", &argparse.Options{Default: "config", Help: HELP_CONFIGFOLDER})
|
configFolder := parser.String("c", "config-folder", &argparse.Options{Default: "config", Help: HELP_CONFIGFOLDER})
|
||||||
@@ -33,6 +37,13 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *signal != "" {
|
||||||
|
client := sockets.Client{}
|
||||||
|
client.OneShotWrite("unix", miniws.SOCKET_PATH, []byte(*signal))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
webserver := miniws.NewWebServer(*port, *logFolder, *configFolder, *wwwFolder, int64(*maxLogBytes))
|
webserver := miniws.NewWebServer(*port, *logFolder, *configFolder, *wwwFolder, int64(*maxLogBytes))
|
||||||
webserver.Run()
|
webserver.Run()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,3 +1,8 @@
|
|||||||
module github.com/shlldev/miniws/miniws
|
module miniws
|
||||||
|
|
||||||
go 1.22.2
|
go 1.22.2
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
|
github.com/wneessen/go-fileperm v0.2.1
|
||||||
|
)
|
||||||
|
@@ -0,0 +1,4 @@
|
|||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/wneessen/go-fileperm v0.2.1 h1:VNZT41b8HJDY5zUw4TbwPtfU1DuxZ3lcGH4dXlaZKis=
|
||||||
|
github.com/wneessen/go-fileperm v0.2.1/go.mod h1:Isv0pfQJstXAlmGGJjLGqCK0Z6d1ehbbrsO2xmTRsKs=
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package miniws
|
package miniws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"mime"
|
"mime"
|
||||||
@@ -8,6 +9,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
|
"sockets"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -18,6 +20,8 @@ import (
|
|||||||
const (
|
const (
|
||||||
FILTER_MODE_WHITELIST FilterMode = 0
|
FILTER_MODE_WHITELIST FilterMode = 0
|
||||||
FILTER_MODE_BLACKLIST FilterMode = 1
|
FILTER_MODE_BLACKLIST FilterMode = 1
|
||||||
|
|
||||||
|
SOCKET_PATH string = "/tmp/miniws_commands_server"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FilterMode int
|
type FilterMode int
|
||||||
@@ -66,13 +70,31 @@ func (ws *WebServer) Run() {
|
|||||||
ws.ipFilterMode, ws.ipFilter = ws.parseFilterPanics(FILENAME_IPFILTER)
|
ws.ipFilterMode, ws.ipFilter = ws.parseFilterPanics(FILENAME_IPFILTER)
|
||||||
ws.userAgentFilterMode, ws.userAgentFilter = ws.parseFilterPanics(FILENAME_USERAGENTFILTER)
|
ws.userAgentFilterMode, ws.userAgentFilter = ws.parseFilterPanics(FILENAME_USERAGENTFILTER)
|
||||||
|
|
||||||
|
socketserver := sockets.Server{}
|
||||||
|
go socketserver.Start(ws.recvBind, "unix", SOCKET_PATH)
|
||||||
|
|
||||||
http.HandleFunc("/", ws.get)
|
http.HandleFunc("/", ws.get)
|
||||||
log.Println("Server started on port " + strconv.Itoa(ws.port))
|
log.Println("Server started on port " + strconv.Itoa(ws.port))
|
||||||
http.ListenAndServe(":"+strconv.Itoa(ws.port), nil)
|
http.ListenAndServe(":"+strconv.Itoa(ws.port), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ws *WebServer) recvBind(command string, arguments []string) byte {
|
||||||
|
command = string(bytes.Trim([]byte(command), "\x00"))
|
||||||
|
switch command {
|
||||||
|
case "reload":
|
||||||
|
ws.parseFilterPanics(FILENAME_IPFILTER)
|
||||||
|
ws.parseFilterPanics(FILENAME_USERAGENTFILTER)
|
||||||
|
return byte(1)
|
||||||
|
default:
|
||||||
|
log.Println("Error: unknown command", command, arguments)
|
||||||
|
return byte(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (ws *WebServer) parseFilterPanics(fileName string) (FilterMode, []string) {
|
func (ws *WebServer) parseFilterPanics(fileName string) (FilterMode, []string) {
|
||||||
|
|
||||||
|
log.Println("loaded filter: ", fileName)
|
||||||
|
|
||||||
filterMode := FILTER_MODE_BLACKLIST
|
filterMode := FILTER_MODE_BLACKLIST
|
||||||
filter := make([]string, 0)
|
filter := make([]string, 0)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user