server add safe close user conn
Signed-off-by: arraykeys@gmail.com <arraykeys@gmail.com>
This commit is contained in:
@ -34,7 +34,8 @@ Proxy is a high performance HTTP, HTTPS, HTTPS, websocket, TCP, UDP, Socks5 prox
|
||||
- ...
|
||||
|
||||
|
||||
This page is the v4.5 manual, and the other version of the manual can be checked by the following link.
|
||||
This page is the v4.6 manual, and the other version of the manual can be checked by the following link.
|
||||
- [v4.5 manual](https://github.com/snail007/goproxy/tree/v4.5)
|
||||
- [v4.4 manual](https://github.com/snail007/goproxy/tree/v4.4)
|
||||
- [v4.3 manual](https://github.com/snail007/goproxy/tree/v4.3)
|
||||
- [v4.2 manual](https://github.com/snail007/goproxy/tree/v4.2)
|
||||
@ -149,7 +150,7 @@ If the installation fails or your VPS is not a linux64 system, please follow the
|
||||
Download address: https://github.com/snail007/goproxy/releases
|
||||
```shell
|
||||
cd /root/proxy/
|
||||
wget https://github.com/snail007/goproxy/releases/download/v4.5/proxy-linux-amd64.tar.gz
|
||||
wget https://github.com/snail007/goproxy/releases/download/v4.6/proxy-linux-amd64.tar.gz
|
||||
```
|
||||
#### **2.Download the automatic installation script**
|
||||
```shell
|
||||
|
||||
@ -35,7 +35,8 @@ Proxy是golang实现的高性能http,https,websocket,tcp,udp,socks5代理服务
|
||||
- ...
|
||||
|
||||
|
||||
本页是v4.5手册,其他版本手册请点击下面链接查看.
|
||||
本页是v4.6手册,其他版本手册请点击下面链接查看.
|
||||
- [v4.5手册](https://github.com/snail007/goproxy/tree/v4.5)
|
||||
- [v4.4手册](https://github.com/snail007/goproxy/tree/v4.4)
|
||||
- [v4.3手册](https://github.com/snail007/goproxy/tree/v4.3)
|
||||
- [v4.2手册](https://github.com/snail007/goproxy/tree/v4.2)
|
||||
@ -147,7 +148,7 @@ curl -L https://raw.githubusercontent.com/snail007/goproxy/master/install_auto.s
|
||||
下载地址:https://github.com/snail007/goproxy/releases
|
||||
```shell
|
||||
cd /root/proxy/
|
||||
wget https://github.com/snail007/goproxy/releases/download/v4.5/proxy-linux-amd64.tar.gz
|
||||
wget https://github.com/snail007/goproxy/releases/download/v4.6/proxy-linux-amd64.tar.gz
|
||||
```
|
||||
#### **2.下载自动安装脚本**
|
||||
```shell
|
||||
|
||||
@ -5,7 +5,7 @@ if [ -e /tmp/proxy ]; then
|
||||
fi
|
||||
mkdir /tmp/proxy
|
||||
cd /tmp/proxy
|
||||
wget https://github.com/snail007/goproxy/releases/download/v4.5/proxy-linux-amd64.tar.gz
|
||||
wget https://github.com/snail007/goproxy/releases/download/v4.6/proxy-linux-amd64.tar.gz
|
||||
|
||||
# #install proxy
|
||||
tar zxvf proxy-linux-amd64.tar.gz
|
||||
|
||||
2
main.go
2
main.go
@ -8,7 +8,7 @@ import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const APP_VERSION = "4.5"
|
||||
const APP_VERSION = "4.6"
|
||||
|
||||
func main() {
|
||||
err := initConfig()
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#!/bin/bash
|
||||
VER="4.5"
|
||||
VER="4.6"
|
||||
RELEASE="release-${VER}"
|
||||
rm -rf .cert
|
||||
mkdir .cert
|
||||
|
||||
@ -11,6 +11,7 @@ import (
|
||||
"snail007/proxy/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/golang/snappy"
|
||||
@ -18,11 +19,13 @@ import (
|
||||
)
|
||||
|
||||
type MuxServer struct {
|
||||
cfg MuxServerArgs
|
||||
udpChn chan MuxUDPItem
|
||||
sc utils.ServerChannel
|
||||
sessions utils.ConcurrentMap
|
||||
lockChn chan bool
|
||||
cfg MuxServerArgs
|
||||
udpChn chan MuxUDPItem
|
||||
sc utils.ServerChannel
|
||||
sessions utils.ConcurrentMap
|
||||
userConns utils.ConcurrentMap
|
||||
lockChn chan bool
|
||||
closeLock *sync.Mutex
|
||||
}
|
||||
|
||||
type MuxServerManager struct {
|
||||
@ -116,10 +119,12 @@ func (s *MuxServerManager) InitService() {
|
||||
|
||||
func NewMuxServer() Service {
|
||||
return &MuxServer{
|
||||
cfg: MuxServerArgs{},
|
||||
udpChn: make(chan MuxUDPItem, 50000),
|
||||
lockChn: make(chan bool, 1),
|
||||
sessions: utils.NewConcurrentMap(),
|
||||
cfg: MuxServerArgs{},
|
||||
udpChn: make(chan MuxUDPItem, 50000),
|
||||
lockChn: make(chan bool, 1),
|
||||
sessions: utils.NewConcurrentMap(),
|
||||
userConns: utils.NewConcurrentMap(),
|
||||
closeLock: &sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
@ -164,6 +169,8 @@ func (s *MuxServer) Start(args interface{}) (err error) {
|
||||
log.Printf("connection handler crashed with err : %s \nstack: %s", err, string(debug.Stack()))
|
||||
}
|
||||
}()
|
||||
inConnRemoteAddr := inConn.RemoteAddr().String()
|
||||
s.userConns.Set(inConnRemoteAddr, &inConn)
|
||||
var outConn net.Conn
|
||||
var ID string
|
||||
for {
|
||||
@ -177,6 +184,9 @@ func (s *MuxServer) Start(args interface{}) (err error) {
|
||||
break
|
||||
}
|
||||
}
|
||||
outConnRemoteAddr := outConn.RemoteAddr().String()
|
||||
s.userConns.Set(outConnRemoteAddr, &outConn)
|
||||
|
||||
log.Printf("%s stream %s created", *s.cfg.Key, ID)
|
||||
if *s.cfg.IsCompress {
|
||||
die1 := make(chan bool, 1)
|
||||
@ -195,9 +205,13 @@ func (s *MuxServer) Start(args interface{}) (err error) {
|
||||
}
|
||||
outConn.Close()
|
||||
inConn.Close()
|
||||
s.userConns.Remove(inConnRemoteAddr)
|
||||
s.userConns.Remove(outConnRemoteAddr)
|
||||
log.Printf("%s stream %s released", *s.cfg.Key, ID)
|
||||
} else {
|
||||
utils.IoBind(inConn, outConn, func(err interface{}) {
|
||||
s.userConns.Remove(inConnRemoteAddr)
|
||||
s.userConns.Remove(outConnRemoteAddr)
|
||||
log.Printf("%s stream %s released", *s.cfg.Key, ID)
|
||||
})
|
||||
}
|
||||
@ -270,6 +284,15 @@ func (s *MuxServer) GetConn(index string) (conn net.Conn, err error) {
|
||||
for {
|
||||
if session.IsClosed() {
|
||||
s.sessions.Remove(index)
|
||||
s.closeLock.Lock()
|
||||
if len(s.userConns) > 0 {
|
||||
for _, k := range s.userConns.Keys() {
|
||||
c, _ := s.userConns.Get(k)
|
||||
(*(c.(*net.Conn))).Close()
|
||||
s.userConns.Remove(k)
|
||||
}
|
||||
}
|
||||
s.closeLock.Unlock()
|
||||
break
|
||||
}
|
||||
time.Sleep(time.Second * 5)
|
||||
|
||||
Reference in New Issue
Block a user