Skip to content

Commit

Permalink
go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ktcunreal committed Sep 2, 2021
1 parent f9828e5 commit 60a6c02
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 86 deletions.
19 changes: 9 additions & 10 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package main

import (
"../config"
"../utils"
"../proxy"
"../utils"
"log"
"net"
)

func main() {
conf := config.LoadClient()
key := utils.NewKey(conf.RAW)

listener := initListener(conf.CLIENT)
defer listener.Close()

Expand Down Expand Up @@ -42,13 +41,13 @@ func initListener(addr string) net.Listener {
func connect(server, client net.Conn, conf *config.Client, key *utils.Key) {
eStream := utils.NewEncStream(server, key)
switch conf.COMPRESSION {
case "none":
proxy.NewProxyClient(client).Forward(eStream)
case "snappy":
cStream := utils.NewSnappyStream(eStream)
proxy.NewProxyClient(client).Forward(cStream)
default:
cStream := utils.NewSnappyStream(eStream)
proxy.NewProxyClient(client).Forward(cStream)
case "none":
proxy.NewProxyClient(client).Forward(eStream)
case "snappy":
cStream := utils.NewSnappyStream(eStream)
proxy.NewProxyClient(client).Forward(cStream)
default:
cStream := utils.NewSnappyStream(eStream)
proxy.NewProxyClient(client).Forward(cStream)
}
}
47 changes: 23 additions & 24 deletions config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,47 @@ package config
import (
"encoding/json"
"flag"
"os"
"log"
"os"
)

type Client struct {
RAW string `json:"key"`
SERVER string `json:"serveraddr"`
CLIENT string `json:"clientaddr"`
RAW string `json:"key"`
SERVER string `json:"serveraddr"`
CLIENT string `json:"clientaddr"`
COMPRESSION string `json:"compression"`
}

func LoadClient() *Client {
config := &Client{}
c := flag.String("c", "./config.json", "Configuration path")
p := flag.String("p","","Pre-shared Key")
s := flag.String("s","127.0.0.1:8000","Server address")
l := flag.String("l","0.0.0.0:9000","Client address")
z := flag.String("z","snappy","Use compression")
p := flag.String("p", "", "Pre-shared Key")
s := flag.String("s", "127.0.0.1:8000", "Server address")
l := flag.String("l", "0.0.0.0:9000", "Client address")
z := flag.String("z", "snappy", "Use compression")
flag.Parse()

log.Printf("LOADING CONFIG FROM %v", *c)
file, err := os.Open(*c)
defer file.Close()

switch err {
case nil:
if err := json.NewDecoder(file).Decode(config); err != nil {
log.Fatalf("PARSE CONFIG ERROR: %v", err)
}
if !validate(config.SERVER) || !validate(config.CLIENT) {
log.Fatalln("Invalid IP ADDRESS")
}
case nil:
if err := json.NewDecoder(file).Decode(config); err != nil {
log.Fatalf("PARSE CONFIG ERROR: %v", err)
}
if !validate(config.SERVER) || !validate(config.CLIENT) {
log.Fatalln("Invalid IP ADDRESS")
}
default:
log.Println("COULD NOT READ CONFIG FROM FILE, PARSING CMDLINE ARGS")
config.RAW = *p
config.COMPRESSION = *z
config.SERVER = *s
config.CLIENT = *l
if !validate(config.SERVER) || !validate(config.CLIENT) {
log.Fatalln("INVALID IP ADDRESS")
}
log.Println("COULD NOT READ CONFIG FROM FILE, PARSING CMDLINE ARGS")
config.RAW = *p
config.COMPRESSION = *z
config.SERVER = *s
config.CLIENT = *l
if !validate(config.SERVER) || !validate(config.CLIENT) {
log.Fatalln("INVALID IP ADDRESS")
}
}
return config
}

48 changes: 24 additions & 24 deletions config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@ package config

import (
"encoding/json"
"log"
"flag"
"log"
"net"
"strings"
"os"
"strconv"
"strings"
)

type Server struct {
RAW string `json:"key"`
SERVER string `json:"serveraddr"`
RAW string `json:"key"`
SERVER string `json:"serveraddr"`
COMPRESSION string `json:"compression"`
}

func LoadServer() *Server {
config := &Server{}
c := flag.String("c", "./config.json", "Configuration path")
p := flag.String("p","","Pre-shared Key")
s := flag.String("s","0.0.0.0:8000","Server address")
z := flag.String("z","snappy","Use compression")
p := flag.String("p", "", "Pre-shared Key")
s := flag.String("s", "0.0.0.0:8000", "Server address")
z := flag.String("z", "snappy", "Use compression")
flag.Parse()

file, err := os.Open(*c)
log.Printf("LOADING CONFIG FROM %v", *c)
defer file.Close()

switch err {
case nil:
if err := json.NewDecoder(file).Decode(config); err != nil {
log.Fatalf("PARSE CONFIG ERROR: %v", err)
}
if !validate(config.SERVER) {
log.Fatalln("Invalid IP ADDRESS")
}
default:
log.Println("COULD NOT READ CONFIG FROM FILE, PARSING CMDLINE ARGS")
config.RAW = *p
config.COMPRESSION = *z
config.SERVER = *s
if !validate(config.SERVER) {
log.Fatalln("INVALID IP ADDRESS")
}
case nil:
if err := json.NewDecoder(file).Decode(config); err != nil {
log.Fatalf("PARSE CONFIG ERROR: %v", err)
}
if !validate(config.SERVER) {
log.Fatalln("Invalid IP ADDRESS")
}
default:
log.Println("COULD NOT READ CONFIG FROM FILE, PARSING CMDLINE ARGS")
config.RAW = *p
config.COMPRESSION = *z
config.SERVER = *s
if !validate(config.SERVER) {
log.Fatalln("INVALID IP ADDRESS")
}
}
return config
}
Expand All @@ -55,11 +55,11 @@ func validate(s string) bool {
return false
}
ip, port = s[:i], s[i+1:]
if len(ip) == 0 || net.ParseIP(ip) == nil{
if len(ip) == 0 || net.ParseIP(ip) == nil {
return false
}
if p, err := strconv.Atoi(port); err != nil || p > 65535 || p < 1 {
return false
}
return true
}
}
10 changes: 5 additions & 5 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (p *ProxyServer) Forward() {
}

length := int(p.rBuf[0])
buf := make([]byte, length + 2)
buf := make([]byte, length+2)
if _, err := io.ReadFull(p.Conn, buf); err != nil {
log.Printf("UNABLE TO GET DST DOMAIN NAME: %v", err)
p.Conn.Close()
Expand All @@ -39,12 +39,12 @@ func (p *ProxyServer) Forward() {
addr := fmt.Sprintf("%s:%d", string(buf[:length]), binary.BigEndian.Uint16(buf[length:]))
log.Printf("CONNECTING: %s", addr)

dst, err := net.DialTimeout("tcp", addr, time.Second * 15)
dst, err := net.DialTimeout("tcp", addr, time.Second*15)
if err != nil {
log.Printf("UNABLE TO CONNECT: %s, %v", addr, err)
p.Conn.Close()
return
return
}

Pipe(p.Conn, dst);
}
Pipe(p.Conn, dst)
}
21 changes: 10 additions & 11 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package main

import (
"../config"
"../utils"
"../proxy"
"../utils"
"log"
"net"
)

func main() {
conf := config.LoadServer()
key := utils.NewKey(conf.RAW)

server := initListener(conf.SERVER)
defer server.Close()

Expand All @@ -36,14 +35,14 @@ func initListener(addr string) net.Listener {

func connect(client net.Conn, conf *config.Server, key *utils.Key) {
eStream := utils.NewEncStream(client, key)
switch conf.COMPRESSION{
case "none":
proxy.NewProxyServer(eStream).Forward()
case "snappy":
cStream := utils.NewSnappyStream(eStream)
proxy.NewProxyServer(cStream).Forward()
default:
cStream := utils.NewSnappyStream(eStream)
proxy.NewProxyServer(cStream).Forward()
switch conf.COMPRESSION {
case "none":
proxy.NewProxyServer(eStream).Forward()
case "snappy":
cStream := utils.NewSnappyStream(eStream)
proxy.NewProxyServer(cStream).Forward()
default:
cStream := utils.NewSnappyStream(eStream)
proxy.NewProxyServer(cStream).Forward()
}
}
1 change: 0 additions & 1 deletion utils/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@ func (s *SnappyStream) Write(p []byte) (n int, err error) {
func (s *SnappyStream) Close() error {
return s.Conn.Close()
}

22 changes: 11 additions & 11 deletions utils/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ import (
const Chunk int = 16384

type Key struct {
hash *[32]byte
salt []byte
pepper []byte
hash *[32]byte
salt []byte
pepper []byte
}

func NewKey(s string) *Key {
h := SH256R(s)
return &Key{
hash: &h,
salt: SH256L(h[:10]),
hash: &h,
salt: SH256L(h[:10]),
pepper: SH256L(h[20:]),
}
}

type EncStream struct {
net.Conn
key *Key
rBuf, dBuf []byte
rNonce, sNonce [24]byte
key *Key
rBuf, dBuf []byte
rNonce, sNonce [24]byte
}

func NewEncStream(conn net.Conn, psk *Key) *EncStream {
return &EncStream{
Conn: conn,
key: psk,
key: psk,
dBuf: make([]byte, 12),
}
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func (e *EncStream) Read(b []byte) (int, error) {
func (e *EncStream) Write(b []byte) (int, error) {
sidx, eidx := 0, 0
for ; sidx < len(b); sidx = eidx {
if len(b) - eidx >= Chunk {
if len(b)-eidx >= Chunk {
eidx += Chunk
} else {
eidx = len(b)
Expand All @@ -100,7 +100,7 @@ func (e *EncStream) Write(b []byte) (int, error) {
return sidx, err
}
}
return sidx, nil
return sidx, nil
}

func (e *EncStream) Close() error {
Expand Down

0 comments on commit 60a6c02

Please sign in to comment.