2016-01-27 21:24:36 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2016-02-03 18:46:24 +08:00
|
|
|
"sync"
|
2016-01-27 21:24:36 +08:00
|
|
|
|
2016-02-03 18:23:30 +08:00
|
|
|
"github.com/fatedier/frp/pkg/utils/conn"
|
|
|
|
"github.com/fatedier/frp/pkg/utils/log"
|
2016-01-27 21:24:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Idle = iota
|
|
|
|
Working
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProxyServer struct {
|
2016-02-03 18:46:24 +08:00
|
|
|
Name string
|
|
|
|
Passwd string
|
|
|
|
BindAddr string
|
|
|
|
ListenPort int64
|
|
|
|
|
2016-02-05 14:18:26 +08:00
|
|
|
Status int64
|
|
|
|
Listener *conn.Listener // accept new connection from remote users
|
|
|
|
CtlMsgChan chan int64 // every time accept a new user conn, put "1" to the channel
|
|
|
|
StopBlockChan chan int64 // put any number to the channel, if you want to stop wait user conn
|
|
|
|
CliConnChan chan *conn.Conn // get client conns from control goroutine
|
|
|
|
UserConnList *list.List // store user conns
|
|
|
|
Mutex sync.Mutex
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProxyServer) Init() {
|
|
|
|
p.Status = Idle
|
|
|
|
p.CtlMsgChan = make(chan int64)
|
2016-02-05 14:18:26 +08:00
|
|
|
p.StopBlockChan = make(chan int64)
|
2016-01-27 21:24:36 +08:00
|
|
|
p.CliConnChan = make(chan *conn.Conn)
|
|
|
|
p.UserConnList = list.New()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProxyServer) Lock() {
|
|
|
|
p.Mutex.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProxyServer) Unlock() {
|
|
|
|
p.Mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// start listening for user conns
|
|
|
|
func (p *ProxyServer) Start() (err error) {
|
|
|
|
p.Listener, err = conn.Listen(p.BindAddr, p.ListenPort)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Status = Working
|
|
|
|
|
|
|
|
// start a goroutine for listener
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
// block
|
2016-02-03 18:46:24 +08:00
|
|
|
c := p.Listener.GetConn()
|
2016-01-27 21:24:36 +08:00
|
|
|
log.Debug("ProxyName [%s], get one new user conn [%s]", p.Name, c.GetRemoteAddr())
|
|
|
|
|
|
|
|
// put to list
|
|
|
|
p.Lock()
|
|
|
|
if p.Status != Working {
|
|
|
|
log.Debug("ProxyName [%s] is not working, new user conn close", p.Name)
|
|
|
|
c.Close()
|
|
|
|
p.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.UserConnList.PushBack(c)
|
|
|
|
p.Unlock()
|
|
|
|
|
|
|
|
// put msg to control conn
|
|
|
|
p.CtlMsgChan <- 1
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// start another goroutine for join two conns from client and user
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
cliConn := <-p.CliConnChan
|
|
|
|
p.Lock()
|
|
|
|
element := p.UserConnList.Front()
|
|
|
|
|
|
|
|
var userConn *conn.Conn
|
|
|
|
if element != nil {
|
|
|
|
userConn = element.Value.(*conn.Conn)
|
|
|
|
p.UserConnList.Remove(element)
|
|
|
|
} 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()
|
|
|
|
p.Status = Idle
|
|
|
|
p.CtlMsgChan = make(chan int64)
|
|
|
|
p.CliConnChan = make(chan *conn.Conn)
|
|
|
|
p.UserConnList = list.New()
|
|
|
|
p.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-02-05 14:18:26 +08:00
|
|
|
func (p *ProxyServer) WaitUserConn() (res int64, isStop bool) {
|
|
|
|
select {
|
|
|
|
case res = <-p.CtlMsgChan:
|
|
|
|
return res, false
|
|
|
|
case <-p.StopBlockChan:
|
|
|
|
return 0, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProxyServer) StopWaitUserConn() {
|
|
|
|
p.StopBlockChan <- 1
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|