Skip to content

Commit

Permalink
start working on l2tp protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Sep 20, 2023
1 parent dbbe1d5 commit aa65c79
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 32 deletions.
19 changes: 3 additions & 16 deletions bufpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@ package wpn
import (
"bytes"
"sync"
)

const ipPacketMaxSize = 65536
var ipBufPool = sync.Pool{
New: func()(any){
buf := make([]byte, ipPacketMaxSize)
return &buf
},
}

func getIPPktBuf()(buf []byte, free func()){
p := ipBufPool.Get().(*[]byte)
return *p, func(){
ipBufPool.Put(p)
}
}
"github.com/kmcsr/wpn/internal/pool"
)

var packetFormatBufPool = sync.Pool{
New: func()(any){
return bytes.NewBuffer(make([]byte, 0, ipPacketMaxSize + 1024))
return bytes.NewBuffer(make([]byte, 0, pool.IPPacketMaxSize + 1024))
},
}
20 changes: 8 additions & 12 deletions client/desktop/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

// Copyright (C) 2023 Kevin Z <[email protected]>
package main

import (
Expand All @@ -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(){
Expand All @@ -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)
}
Expand All @@ -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)
}
}()

Expand Down
7 changes: 4 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"nhooyr.io/websocket"
"github.com/kmcsr/wpn/internal/pool"
)

var (
Expand Down Expand Up @@ -326,7 +327,7 @@ func (c *Conn)handleBinary(r io.Reader)(err error){
data := &packetDataT{
addr: &addr,
}
buf, data.freeBuf = getIPPktBuf()
buf, data.freeBuf = pool.GetIPPacketBuf()
var n int
if n, err = br.Read(buf); err != nil {
if errors.Is(err, io.EOF) {
Expand Down Expand Up @@ -533,7 +534,7 @@ func (c *Conn)serveStream()(err error){
return
}
defer conn.Close()
buf, freeBuf := getIPPktBuf()
buf, freeBuf := pool.GetIPPacketBuf()
defer freeBuf()
for {
var n int
Expand Down Expand Up @@ -582,7 +583,7 @@ func (c *Conn)servePacket()(err error){
return
}
defer conn.Close()
buf, freeBuf := getIPPktBuf()
buf, freeBuf := pool.GetIPPacketBuf()
defer freeBuf()
for {
var (
Expand Down
21 changes: 21 additions & 0 deletions internal/pool/ippkt.go
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)
}
}
72 changes: 72 additions & 0 deletions l2tp/errors.go
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
}
Loading

0 comments on commit aa65c79

Please sign in to comment.