2016-02-18 16:56:55 +08:00
|
|
|
package server
|
2016-01-27 21:24:36 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2016-02-03 18:46:24 +08:00
|
|
|
"sync"
|
2016-01-27 21:24:36 +08:00
|
|
|
|
2016-02-18 16:56:55 +08:00
|
|
|
"github.com/fatedier/frp/models/consts"
|
|
|
|
"github.com/fatedier/frp/utils/conn"
|
|
|
|
"github.com/fatedier/frp/utils/log"
|
2016-01-27 21:24:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProxyServer struct {
|
2016-02-19 17:01:47 +08:00
|
|
|
Name string
|
|
|
|
Passwd string
|
|
|
|
BindAddr string
|
|
|
|
ListenPort int64
|
|
|
|
Status int64
|
|
|
|
CliConnChan chan *conn.Conn // get client conns from control goroutine
|
|
|
|
|
|
|
|
listener *conn.Listener // accept new connection from remote users
|
|
|
|
ctlMsgChan chan int64 // every time accept a new user conn, put "1" to the channel
|
|
|
|
userConnList *list.List // store user conns
|
|
|
|
mutex sync.Mutex
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProxyServer) Init() {
|
2016-02-18 16:56:55 +08:00
|
|
|
p.Status = consts.Idle
|
2016-01-27 21:24:36 +08:00
|
|
|
p.CliConnChan = make(chan *conn.Conn)
|
2016-02-19 17:01:47 +08:00
|
|
|
p.ctlMsgChan = make(chan int64)
|
|
|
|
p.userConnList = list.New()
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProxyServer) Lock() {
|
2016-02-19 17:01:47 +08:00
|
|
|
p.mutex.Lock()
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProxyServer) Unlock() {
|
2016-02-19 17:01:47 +08:00
|
|
|
p.mutex.Unlock()
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// start listening for user conns
|
|
|
|
func (p *ProxyServer) Start() (err error) {
|
2016-02-19 17:01:47 +08:00
|
|
|
p.Init()
|
|
|
|
p.listener, err = conn.Listen(p.BindAddr, p.ListenPort)
|
2016-01-27 21:24:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-18 16:56:55 +08:00
|
|
|
p.Status = consts.Working
|
2016-01-27 21:24:36 +08:00
|
|
|
|
|
|
|
// start a goroutine for listener
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
// block
|
2016-02-19 17:01:47 +08:00
|
|
|
// if listener is closed, get nil
|
|
|
|
c := p.listener.GetConn()
|
|
|
|
if c == nil {
|
|
|
|
log.Info("ProxyName [%s], listener is closed", p.Name)
|
|
|
|
return
|
|
|
|
}
|
2016-01-27 21:24:36 +08:00
|
|
|
log.Debug("ProxyName [%s], get one new user conn [%s]", p.Name, c.GetRemoteAddr())
|
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
// insert into list
|
2016-01-27 21:24:36 +08:00
|
|
|
p.Lock()
|
2016-02-18 16:56:55 +08:00
|
|
|
if p.Status != consts.Working {
|
2016-01-27 21:24:36 +08:00
|
|
|
log.Debug("ProxyName [%s] is not working, new user conn close", p.Name)
|
|
|
|
c.Close()
|
|
|
|
p.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2016-02-19 17:01:47 +08:00
|
|
|
p.userConnList.PushBack(c)
|
2016-01-27 21:24:36 +08:00
|
|
|
p.Unlock()
|
|
|
|
|
|
|
|
// put msg to control conn
|
2016-02-19 17:01:47 +08:00
|
|
|
p.ctlMsgChan <- 1
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// start another goroutine for join two conns from client and user
|
|
|
|
go func() {
|
|
|
|
for {
|
2016-02-19 17:01:47 +08:00
|
|
|
cliConn, ok := <-p.CliConnChan
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
p.Lock()
|
2016-02-19 17:01:47 +08:00
|
|
|
element := p.userConnList.Front()
|
2016-01-27 21:24:36 +08:00
|
|
|
|
|
|
|
var userConn *conn.Conn
|
|
|
|
if element != nil {
|
|
|
|
userConn = element.Value.(*conn.Conn)
|
2016-02-19 17:01:47 +08:00
|
|
|
p.userConnList.Remove(element)
|
2016-01-27 21:24:36 +08:00
|
|
|
} else {
|
|
|
|
cliConn.Close()
|
2016-02-05 16:49:52 +08:00
|
|
|
p.Unlock()
|
2016-01-27 21:24:36 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.Unlock()
|
|
|
|
|
|
|
|
// msg will transfer to another without modifying
|
2016-02-18 11:42:31 +08:00
|
|
|
// l means local, r means remote
|
2016-01-27 21:24:36 +08:00
|
|
|
log.Debug("Join two conns, (l[%s] r[%s]) (l[%s] r[%s])", cliConn.GetLocalAddr(), cliConn.GetRemoteAddr(),
|
2016-02-03 18:46:24 +08:00
|
|
|
userConn.GetLocalAddr(), userConn.GetRemoteAddr())
|
2016-01-27 21:24:36 +08:00
|
|
|
go conn.Join(cliConn, userConn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProxyServer) Close() {
|
|
|
|
p.Lock()
|
2016-02-18 16:56:55 +08:00
|
|
|
p.Status = consts.Idle
|
2016-02-19 17:01:47 +08:00
|
|
|
p.listener.Close()
|
|
|
|
close(p.ctlMsgChan)
|
|
|
|
close(p.CliConnChan)
|
|
|
|
p.userConnList = list.New()
|
2016-01-27 21:24:36 +08:00
|
|
|
p.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
func (p *ProxyServer) WaitUserConn() (closeFlag bool) {
|
|
|
|
closeFlag = false
|
2016-02-05 14:18:26 +08:00
|
|
|
|
2016-02-19 17:01:47 +08:00
|
|
|
_, ok := <-p.ctlMsgChan
|
|
|
|
if !ok {
|
|
|
|
closeFlag = true
|
|
|
|
}
|
|
|
|
return
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|