server add safe close user conn

Signed-off-by: arraykeys@gmail.com <arraykeys@gmail.com>
This commit is contained in:
arraykeys@gmail.com
2018-03-13 18:36:34 +08:00
parent 0d85c7dd7d
commit bab4325414
6 changed files with 41 additions and 16 deletions

View File

@ -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.4 manual](https://github.com/snail007/goproxy/tree/v4.4)
- [v4.3 manual](https://github.com/snail007/goproxy/tree/v4.3) - [v4.3 manual](https://github.com/snail007/goproxy/tree/v4.3)
- [v4.2 manual](https://github.com/snail007/goproxy/tree/v4.2) - [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 Download address: https://github.com/snail007/goproxy/releases
```shell ```shell
cd /root/proxy/ 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** #### **2.Download the automatic installation script**
```shell ```shell

View File

@ -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.4手册](https://github.com/snail007/goproxy/tree/v4.4)
- [v4.3手册](https://github.com/snail007/goproxy/tree/v4.3) - [v4.3手册](https://github.com/snail007/goproxy/tree/v4.3)
- [v4.2手册](https://github.com/snail007/goproxy/tree/v4.2) - [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 下载地址:https://github.com/snail007/goproxy/releases
```shell ```shell
cd /root/proxy/ 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.下载自动安装脚本** #### **2.下载自动安装脚本**
```shell ```shell

View File

@ -5,7 +5,7 @@ if [ -e /tmp/proxy ]; then
fi fi
mkdir /tmp/proxy mkdir /tmp/proxy
cd /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 # #install proxy
tar zxvf proxy-linux-amd64.tar.gz tar zxvf proxy-linux-amd64.tar.gz

View File

@ -8,7 +8,7 @@ import (
"syscall" "syscall"
) )
const APP_VERSION = "4.5" const APP_VERSION = "4.6"
func main() { func main() {
err := initConfig() err := initConfig()

View File

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
VER="4.5" VER="4.6"
RELEASE="release-${VER}" RELEASE="release-${VER}"
rm -rf .cert rm -rf .cert
mkdir .cert mkdir .cert

View File

@ -11,6 +11,7 @@ import (
"snail007/proxy/utils" "snail007/proxy/utils"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/golang/snappy" "github.com/golang/snappy"
@ -22,7 +23,9 @@ type MuxServer struct {
udpChn chan MuxUDPItem udpChn chan MuxUDPItem
sc utils.ServerChannel sc utils.ServerChannel
sessions utils.ConcurrentMap sessions utils.ConcurrentMap
userConns utils.ConcurrentMap
lockChn chan bool lockChn chan bool
closeLock *sync.Mutex
} }
type MuxServerManager struct { type MuxServerManager struct {
@ -120,6 +123,8 @@ func NewMuxServer() Service {
udpChn: make(chan MuxUDPItem, 50000), udpChn: make(chan MuxUDPItem, 50000),
lockChn: make(chan bool, 1), lockChn: make(chan bool, 1),
sessions: utils.NewConcurrentMap(), 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())) 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 outConn net.Conn
var ID string var ID string
for { for {
@ -177,6 +184,9 @@ func (s *MuxServer) Start(args interface{}) (err error) {
break break
} }
} }
outConnRemoteAddr := outConn.RemoteAddr().String()
s.userConns.Set(outConnRemoteAddr, &outConn)
log.Printf("%s stream %s created", *s.cfg.Key, ID) log.Printf("%s stream %s created", *s.cfg.Key, ID)
if *s.cfg.IsCompress { if *s.cfg.IsCompress {
die1 := make(chan bool, 1) die1 := make(chan bool, 1)
@ -195,9 +205,13 @@ func (s *MuxServer) Start(args interface{}) (err error) {
} }
outConn.Close() outConn.Close()
inConn.Close() inConn.Close()
s.userConns.Remove(inConnRemoteAddr)
s.userConns.Remove(outConnRemoteAddr)
log.Printf("%s stream %s released", *s.cfg.Key, ID) log.Printf("%s stream %s released", *s.cfg.Key, ID)
} else { } else {
utils.IoBind(inConn, outConn, func(err interface{}) { 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) 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 { for {
if session.IsClosed() { if session.IsClosed() {
s.sessions.Remove(index) 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 break
} }
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)