Skip to content

Commit

Permalink
add level field
Browse files Browse the repository at this point in the history
  • Loading branch information
若尘(樱の泪) committed Feb 10, 2019
1 parent cdeb7e9 commit 03b156e
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 212 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ server.exe
vnet
vnet.exe
config.json
*.test.exe
*.test.exe
*.pprof
31 changes: 18 additions & 13 deletions cmd/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ package main

import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)

func main() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
for {
data := <-c
fmt.Println(data.String())
if data == os.Interrupt {
return
}
}

go func() {
defer func() {
if e := recover(); e != nil {
fmt.Printf("error %v", e)
}
}()
go func() {
defer func() {
if e := recover(); e != nil {
fmt.Printf("error %v \n", e)
}
}()
panic("this is error")
}()
}()
time.Sleep(1 * time.Second)
fmt.Println("aaa")
}
17 changes: 17 additions & 0 deletions cmd/test/test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import "testing"

func Benchmark_panic(t *testing.B) {
t.ResetTimer()
for i := 0; i < t.N; i++ {
func() {
defer func() {
if e := recover(); e != nil {

}
}()
panic(100)
}()
}
}
3 changes: 3 additions & 0 deletions common/auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package auth

// TODO socks5 auth
14 changes: 10 additions & 4 deletions common/ciphers/ssstream/cipher_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package ssstream

import (
"crypto/rand"
"errors"
"fmt"
"io"
"net"
"sync"

"github.com/pkg/errors"
"github.com/rc452860/vnet/common/pool"
)

Expand Down Expand Up @@ -42,7 +43,7 @@ func GetStreamPacketCiphers(method string) func(string, net.PacketConn) (net.Pac
}
}

func (c *streamPacket) WriteTo(b []byte, addr net.Addr) (int, error) {
func (c *streamPacket) WriteTo(b []byte, addr net.Addr) (n int, err error) {
c.Lock()
defer c.Unlock()
ivLen := c.IVLen()
Expand All @@ -63,8 +64,13 @@ func (c *streamPacket) WriteTo(b []byte, addr net.Addr) (int, error) {
return dataLen, nil
}

func (c *streamPacket) ReadFrom(b []byte) (int, net.Addr, error) {
n, addr, err := c.PacketConn.ReadFrom(b)
func (c *streamPacket) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
defer func() {
if e := recover(); e != nil {
err = errors.WithStack(errors.New(fmt.Sprintf("%v", e)))
}
}()
n, addr, err = c.PacketConn.ReadFrom(b)

if err != nil {
return n, addr, err
Expand Down
7 changes: 5 additions & 2 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/rc452860/vnet/common/log"
"github.com/rc452860/vnet/utils"
"github.com/rc452860/vnet/utils/iox"
)

var (
Expand Down Expand Up @@ -40,6 +41,7 @@ type DbConfig struct {
NodeId int `json:"node_id`
SyncTime int `json:"sync_time"`
OnlineSyncTime int `json:"online_sync_time"`
Level int `json:"level"`
}

// ShadowsocksOptions is global shadowoscks service config
Expand All @@ -56,7 +58,8 @@ func DefaultConfig() *Config {
return &Config{
Mode: "db",
DbConfig: DbConfig{
Rate: -1,
Level: -1,
Rate: -1,
},
ShadowsocksOptions: ShadowsocksOptions{
ConnectTimeout: 3000,
Expand Down Expand Up @@ -89,7 +92,7 @@ func LoadDefault() (*Config, error) {
func LoadConfig(file string) (*Config, error) {
utils.RLock(file)
defer utils.RUnLock(file)
if !utils.IsFileExist(file) {
if !iox.IsFileExist(file) {
absFile, err := filepath.Abs(file)
if err != nil {
log.Err(err)
Expand Down
40 changes: 31 additions & 9 deletions common/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,46 @@ import "sync"

const BufferSize = 4108

var (
poolMap map[int]*sync.Pool
)

func init() {
pool = &sync.Pool{
New: func() interface{} {
return make([]byte, BufferSize)
},
}
poolMap = make(map[int]*sync.Pool)
}

var pool *sync.Pool

func GetBuf() []byte {
buf := pool.Get().([]byte)
pool := poolMap[BufferSize]
if pool == nil {
poolMap[BufferSize] = &sync.Pool{
New: createAllocFunc(BufferSize),
}
}
buf := poolMap[BufferSize].Get().([]byte)
buf = buf[:cap(buf)]
return buf
}

func GetBufBySize(size int) []byte {
pool := poolMap[size]
if pool == nil {
poolMap[size] = &sync.Pool{
New: createAllocFunc(size),
}
}
buf := poolMap[size].Get().([]byte)
buf = buf[:cap(buf)]
return buf
}

func PutBuf(buf []byte) {
pool.Put(buf)
poolMap[cap(buf)].Put(buf)
}

func createAllocFunc(size int) func() interface{} {
return func() interface{} {
return make([]byte, size)
}
}

// type BytesPool struct {
Expand Down
22 changes: 21 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ func GetEnableUser() ([]User, error) {
return userList, nil
}

func GetUserWithLevel(level int) ([]User, error) {
connect, err := Connect()
if err != nil {
return nil, err
}
defer connect.Close()

var userList []User
connect.Where("port != 0 AND enable = 1 AND level = level").Find(&userList)
return userList, nil
}

func DbStarted(ctx context.Context) {
conf := config.CurrentConfig()
if conf.DbConfig.Host == "" {
Expand Down Expand Up @@ -238,7 +250,15 @@ func DBServiceMonitor(ctx context.Context) {
return
default:
}
users, err := GetEnableUser()
var (
users []User
err error
)
if conf.Level != -1 {
users, err = GetEnableUser()
} else {
users, err = GetUserWithLevel(conf.Level)
}
if err != nil {
log.Err(err)
continue
Expand Down
19 changes: 9 additions & 10 deletions proxy/server/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/rc452860/vnet/component/dnsx"
"github.com/rc452860/vnet/utils/addr"

"github.com/pkg/errors"

Expand Down Expand Up @@ -157,8 +158,8 @@ func (s *ShadowsocksProxy) tcpDownload(con conn.IConn, down uint64) {

// start shadowsocks tcp proxy service
func (s *ShadowsocksProxy) startTCP() error {
addr := fmt.Sprintf("%s:%d", s.Host, s.Port)
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
serverAddr := fmt.Sprintf("%s:%d", s.Host, s.Port)
tcpAddr, err := net.ResolveTCPAddr("tcp", serverAddr)
if err != nil {
logging.Error(err.Error())
return err
Expand Down Expand Up @@ -220,15 +221,15 @@ func (s *ShadowsocksProxy) startTCP() error {
/** 读取目标地址 */
targetAddr, err := socks.ReadAddr(lcd)
if err != nil {
log.Error("read target address error %s. (maybe the crypto method wrong configuration)", err.Error())
log.Error("tcp:%v read target address error %s. (maybe the crypto method wrong configuration)", addr.GetPortFromAddr(server.Addr()), err.Error())
return
}
addr, err := s.dnsReslove(targetAddr)
resloveAddr, err := s.dnsReslove(targetAddr)
if err != nil {
log.Err(err)
return
}
rc, err := net.Dial("tcp", addr)
rc, err := net.Dial("tcp", resloveAddr)
if err != nil {
logging.Error("connect target:%s error cause: %v", targetAddr, err)
return
Expand All @@ -238,7 +239,6 @@ func (s *ShadowsocksProxy) startTCP() error {
s.ConnectionStage(s.TCP.Addr(), lcd.RemoteAddr(), rc.RemoteAddr(), targetAddr)

rc.(*net.TCPConn).SetKeepAlive(true)
// logging.Info("tcp %s <----> %s", lcd.RemoteAddr(), targetAddr)

/** 默认装饰器 */
rcd, err := conn.DefaultDecorate(rc, conn.TCP)
Expand Down Expand Up @@ -316,8 +316,8 @@ func (s *ShadowsocksProxy) udpDownload(laddr, raddr net.Addr, n uint64) {

// Listen on addr for encrypted packets and basically do UDP NAT.
func (s *ShadowsocksProxy) startUDP() error {
addr := fmt.Sprintf("%s:%d", s.Host, s.Port)
server, err := net.ListenPacket("udp", addr)
serverAddr := fmt.Sprintf("%s:%d", s.Host, s.Port)
server, err := net.ListenPacket("udp", serverAddr)
if err != nil {
logging.Error("UDP remote listen error: %v", err)
return errors.Cause(err)
Expand Down Expand Up @@ -359,15 +359,14 @@ func (s *ShadowsocksProxy) startUDP() error {
}
tgtAddr := socks.SplitAddr(buf[:n])
if tgtAddr == nil {
logging.Error("failed to split target address from packet: %q", buf[:n])
logging.Error("udp:%v read target address error. (maybe the crypto method wrong configuration)", addr.GetPortFromAddr(server.LocalAddr()))
continue
}
addr, err := s.dnsReslove(tgtAddr)
if err != nil {
log.Err(err)
return
}
// logging.Info("udp %s <----> %s", raddr, tgtAddr)
tgtUDPAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
logging.Error("failed to resolve target UDP address: %v", err)
Expand Down
Loading

0 comments on commit 03b156e

Please sign in to comment.