-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
353 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
|
||
// Copyright (C) 2023 Kevin Z <[email protected]> | ||
package main | ||
|
||
import ( | ||
|
@@ -11,8 +12,7 @@ import ( | |
"time" | ||
|
||
"github.com/kmcsr/wpn" | ||
"github.com/kmcsr/wpn/socks5" | ||
"github.com/kmcsr/wpn/wssocks" | ||
"github.com/kmcsr/wpn/l2tp" | ||
) | ||
|
||
func main(){ | ||
|
@@ -23,7 +23,7 @@ func main(){ | |
{ | ||
ping, err := client.Ping() | ||
if err != nil { | ||
loger.Fatalf("Cannot ping the server: %v", err) | ||
// loger.Fatalf("Cannot ping the server: %v", err) | ||
} | ||
loger.Infof("Connected to the server: ping=%v", ping) | ||
} | ||
|
@@ -42,20 +42,16 @@ func main(){ | |
} | ||
} | ||
}() | ||
shandler := &wssocks.Handler{ | ||
Client: client, | ||
} | ||
server := &socks5.Server{ | ||
Addr: config.SocksAddr, | ||
Handler: shandler, | ||
DialTimeout: time.Second * 30, | ||
server := &l2tp.Server{ | ||
// Addr: config.SocksAddr, | ||
Logger: loger, | ||
} | ||
|
||
go func(){ | ||
defer close(done) | ||
loger.Infof("Starting socks5 server on %q", server.Addr) | ||
loger.Infof("Starting L2TP server at %q", server.Addr) | ||
if err := server.ListenAndServe(); err != nil && !errors.Is(err, net.ErrClosed) { | ||
loger.Fatalf("Error when running socks5 server: %v", err) | ||
loger.Fatalf("Error when running L2TP server: %v", err) | ||
} | ||
}() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
package pool | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
const IPPacketMaxSize = 65536 | ||
var ipBufPool = sync.Pool{ | ||
New: func()(any){ | ||
buf := make([]byte, IPPacketMaxSize) | ||
return &buf | ||
}, | ||
} | ||
|
||
func GetIPPacketBuf()(buf []byte, free func()){ | ||
p := ipBufPool.Get().(*[]byte) | ||
return *p, func(){ | ||
ipBufPool.Put(p) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
package l2tp | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"runtime" | ||
"sync" | ||
) | ||
|
||
var WrongLengthErr = errors.New("Wrong length of the packet") | ||
|
||
type VersionError struct { | ||
Version byte | ||
} | ||
|
||
func (e *VersionError)Error()(string){ | ||
return fmt.Sprintf("Unsupport version %d, support %d", e.Version, L2TPVersion) | ||
} | ||
|
||
type UnexpectFlagValue struct { | ||
Flag string | ||
Expect bool | ||
} | ||
|
||
func (e *UnexpectFlagValue)Error()(string){ | ||
if e.Expect { | ||
return fmt.Sprintf("Flag %s is expected to set", e.Flag) | ||
} | ||
return fmt.Sprintf("Flag %s is unexpected to set", e.Flag) | ||
} | ||
|
||
type PanicError struct { | ||
err any | ||
stacktrace string | ||
} | ||
|
||
var stackBufPool = sync.Pool{ | ||
New: func()(any){ | ||
buf := make([]byte, 1024) | ||
return &buf | ||
}, | ||
} | ||
|
||
func getStack()(string){ | ||
buf := *(stackBufPool.Get().(*[]byte)) | ||
for { | ||
n := runtime.Stack(buf, false) | ||
if n < len(buf) { | ||
s := (string)(buf[:n]) | ||
stackBufPool.Put(&buf) | ||
return s | ||
} | ||
stackBufPool.Put(&buf) | ||
buf = make([]byte, len(buf) + 1024) | ||
} | ||
} | ||
|
||
func recoverAsError()(*PanicError){ | ||
err := recover() | ||
if err == nil { | ||
return nil | ||
} | ||
return &PanicError{ | ||
err: err, | ||
stacktrace: getStack(), | ||
} | ||
} | ||
|
||
func (e *PanicError)Error()(string){ | ||
return fmt.Sprintf("panic: %v\n", e.err) + e.stacktrace | ||
} |
Oops, something went wrong.