frp/models/server/server.go

131 lines
2.7 KiB
Go
Raw Normal View History

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
"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 {
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() {
p.Status = consts.Idle
2016-01-27 21:24:36 +08:00
p.CliConnChan = make(chan *conn.Conn)
p.ctlMsgChan = make(chan int64)
p.userConnList = list.New()
2016-01-27 21:24:36 +08:00
}
func (p *ProxyServer) Lock() {
p.mutex.Lock()
2016-01-27 21:24:36 +08:00
}
func (p *ProxyServer) Unlock() {
p.mutex.Unlock()
2016-01-27 21:24:36 +08:00
}
// start listening for user conns
func (p *ProxyServer) Start() (err error) {
p.Init()
p.listener, err = conn.Listen(p.BindAddr, p.ListenPort)
2016-01-27 21:24:36 +08:00
if err != nil {
return err
}
p.Status = consts.Working
2016-01-27 21:24:36 +08:00
// start a goroutine for listener
go func() {
for {
// block
// 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())
// insert into list
2016-01-27 21:24:36 +08:00
p.Lock()
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
}
p.userConnList.PushBack(c)
2016-01-27 21:24:36 +08:00
p.Unlock()
// put msg to control conn
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 {
cliConn, ok := <-p.CliConnChan
if !ok {
return
}
2016-01-27 21:24:36 +08:00
p.Lock()
element := p.userConnList.Front()
2016-01-27 21:24:36 +08:00
var userConn *conn.Conn
if element != nil {
userConn = element.Value.(*conn.Conn)
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
// 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 = consts.Idle
p.listener.Close()
close(p.ctlMsgChan)
close(p.CliConnChan)
p.userConnList = list.New()
2016-01-27 21:24:36 +08:00
p.Unlock()
}
func (p *ProxyServer) WaitUserConn() (closeFlag bool) {
closeFlag = false
2016-02-05 14:18:26 +08:00
_, ok := <-p.ctlMsgChan
if !ok {
closeFlag = true
}
return
2016-01-27 21:24:36 +08:00
}