use logging

This commit is contained in:
Cyberes 2024-06-27 17:30:09 -06:00
parent 890a3f527b
commit 059c3514b4
7 changed files with 76 additions and 32 deletions

View File

@ -1,6 +1,6 @@
chacha_key: example123 chacha_key: example123
destinations: destinations:
5170: 5171:
address: 192.168.1.200 address: 192.168.1.200
port: 8080 port: 5171

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash
echo "Key: $(openssl rand -hex 32)" openssl rand -hex 32

View File

@ -3,6 +3,7 @@ module server
go 1.22.2 go 1.22.2
require ( require (
github.com/sirupsen/logrus v1.9.3
golang.org/x/crypto v0.24.0 golang.org/x/crypto v0.24.0
gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v2 v2.4.0
) )

View File

@ -1,8 +1,21 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

27
server/logging/logging.go Normal file
View File

@ -0,0 +1,27 @@
package logging
import (
"github.com/sirupsen/logrus"
)
var log *logrus.Logger
func init() {
log = logrus.New()
// Set log output format
customFormatter := new(logrus.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
customFormatter.FullTimestamp = true
log.SetFormatter(customFormatter)
}
// InitLogger initializes the global logger with the specified log level
func InitLogger(logLevel logrus.Level) {
log.SetLevel(logLevel)
}
// GetLogger returns the global logger instance
func GetLogger() *logrus.Logger {
return log
}

View File

@ -4,10 +4,12 @@ import (
"encoding/hex" "encoding/hex"
"flag" "flag"
"fmt" "fmt"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"net" "net"
"os" "os"
"server/encryption" "server/encryption"
"server/logging"
) )
type Config struct { type Config struct {
@ -24,35 +26,33 @@ func main() {
configFile := flag.String("config", "", "Path to the configuration file") configFile := flag.String("config", "", "Path to the configuration file")
flag.Parse() flag.Parse()
logging.InitLogger(logrus.InfoLevel)
logger := logging.GetLogger()
if *configFile == "" { if *configFile == "" {
fmt.Println("Please provide a configuration file") logger.Fatalln("Please provide a configuration file")
os.Exit(1)
} }
data, err := os.ReadFile(*configFile) data, err := os.ReadFile(*configFile)
if err != nil { if err != nil {
fmt.Println("Error reading the configuration file:", err) logger.Fatalln("Error reading the configuration file:", err)
os.Exit(1)
} }
var config Config var config Config
err = yaml.Unmarshal(data, &config) err = yaml.Unmarshal(data, &config)
if err != nil { if err != nil {
fmt.Println("Error parsing the configuration file:", err) logger.Fatalln("Error parsing the configuration file:", err)
os.Exit(1)
} }
// Validate chacha key // Validate chacha key
if len(config.ChachaKey) != 64 { if len(config.ChachaKey) != 64 {
fmt.Println("Invalid chacha_key. Should be 64 characters long") logger.Fatalln("Invalid chacha_key. Should be 64 characters long")
os.Exit(1)
} }
// Validate destinations // Validate destinations
for port, dest := range config.Destinations { for port, dest := range config.Destinations {
if dest.Address == "" || dest.Port == 0 { if dest.Address == "" || dest.Port == 0 {
fmt.Printf("Invalid destination for port %s\n", port) logger.Fatalln("Invalid destination for port %s\n", port)
os.Exit(1)
} }
} }
@ -62,13 +62,13 @@ func main() {
go func(port string, dest Destination) { go func(port string, dest Destination) {
addr, err := net.ResolveUDPAddr("udp", ":"+port) addr, err := net.ResolveUDPAddr("udp", ":"+port)
if err != nil { if err != nil {
fmt.Println("Error resolving address:", err) logger.Fatalln("Error resolving address:", err)
return return
} }
conn, err := net.ListenUDP("udp", addr) conn, err := net.ListenUDP("udp", addr)
if err != nil { if err != nil {
fmt.Println("Error listening on UDP:", err) logger.Fatalln("Error listening on UDP:", err)
return return
} }
defer conn.Close() defer conn.Close()
@ -76,39 +76,42 @@ func main() {
// Address to forward the decrypted messages // Address to forward the decrypted messages
forwardAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", dest.Address, dest.Port)) forwardAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", dest.Address, dest.Port))
if err != nil { if err != nil {
fmt.Println("Error resolving forward address:", err) logger.Fatalln("Error resolving forward address:", err)
return return
} }
forwardConn, err := net.DialUDP("udp", nil, forwardAddr) forwardConn, err := net.DialUDP("udp", nil, forwardAddr)
if err != nil { if err != nil {
fmt.Println("Error dialing to forward address:", err) logger.Errorln("Error dialing to forward address:", err)
return return
} }
defer forwardConn.Close() defer forwardConn.Close()
logger.Infof("Listening on 0.0.0.0:%s\n", port)
for { for {
buf := make([]byte, 1500) // 1500 is the standard internet MTU buf := make([]byte, 1500) // 1500 is the standard internet MTU
n, addr, err := conn.ReadFromUDP(buf) n, addr, err := conn.ReadFromUDP(buf)
if err != nil { if err != nil {
fmt.Println("Error reading from UDP:", err) logger.Errorln("Error reading from UDP:", err)
return return
} }
plaintext, err := encryption.Decrypt(key, buf[:n]) // Use only the part of the buffer that has data
if err != nil {
fmt.Println("Error decrypting message:", err)
return
}
fmt.Printf("%s -- %s\n", addr.IP, string(plaintext))
// Forward the decrypted message // Forward the decrypted message
_, err = forwardConn.Write(plaintext) go func(addr *net.UDPAddr, buf []byte, n int) {
if err != nil { plaintext, err := encryption.Decrypt(key, buf[:n]) // Use only the part of the buffer that has data
fmt.Println("Error forwarding message:", err) if err != nil {
return fmt.Println("Error decrypting message:", err)
} return
}
_, err = forwardConn.Write(plaintext)
if err != nil {
fmt.Println("Error forwarding message:", err)
return
}
logger.Infof("%s -> %s:%d -- %s\n", addr.IP, dest.Address, dest.Port, string(plaintext))
}(addr, buf, n)
} }
}(port, dest) }(port, dest)
} }

View File

@ -4,7 +4,7 @@ import time
from Crypto.Cipher import ChaCha20_Poly1305 from Crypto.Cipher import ChaCha20_Poly1305
# The server's address and port # The server's address and port
server_address = ('localhost', 5170) server_address = ('localhost', 5171)
# Create a UDP socket # Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)