frp/src/frp/models/server/server.go

168 lines
3.8 KiB
Go
Raw Normal View History

2016-03-14 11:18:24 +08:00
// Copyright 2016 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
2016-01-27 21:24:36 +08:00
import (
"container/list"
2016-02-03 18:46:24 +08:00
"sync"
2016-02-25 10:28:34 +08:00
"time"
2016-01-27 21:24:36 +08:00
"frp/models/consts"
"frp/utils/conn"
"frp/utils/log"
2016-01-27 21:24:36 +08:00
)
type ProxyServer struct {
2016-02-25 10:28:34 +08:00
Name string
Passwd string
BindAddr string
ListenPort int64
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
2016-03-31 18:03:44 +08:00
workConnChan chan *conn.Conn // get new work conns from control goroutine
2016-02-25 10:28:34 +08:00
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-03-31 18:03:44 +08:00
p.workConnChan = 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
2016-02-25 10:28:34 +08:00
// start a goroutine for listener to accept user connection
2016-01-27 21:24:36 +08:00
go func() {
for {
// block
2016-02-25 10:28:34 +08:00
// if listener is closed, err returned
c, err := p.listener.GetConn()
if err != 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-02-25 10:28:34 +08:00
// set timeout
time.AfterFunc(time.Duration(UserConnTimeout)*time.Second, func() {
p.Lock()
defer p.Unlock()
element := p.userConnList.Front()
if element == nil {
return
}
userConn := element.Value.(*conn.Conn)
if userConn == c {
log.Warn("ProxyName [%s], user conn [%s] timeout", p.Name, c.GetRemoteAddr())
}
})
2016-01-27 21:24:36 +08:00
}
}()
// start another goroutine for join two conns from client and user
go func() {
for {
2016-03-31 18:03:44 +08:00
workConn, ok := <-p.workConnChan
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 {
2016-03-31 18:03:44 +08:00
workConn.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-03-31 18:03:44 +08:00
log.Debug("Join two connections, (l[%s] r[%s]) (l[%s] r[%s])", workConn.GetLocalAddr(), workConn.GetRemoteAddr(),
2016-02-03 18:46:24 +08:00
userConn.GetLocalAddr(), userConn.GetRemoteAddr())
2016-03-31 18:03:44 +08:00
// go conn.Join(workConn, userConn)
go conn.JoinMore(userConn, workConn, p.Passwd)
2016-01-27 21:24:36 +08:00
}
}()
return nil
}
func (p *ProxyServer) Close() {
p.Lock()
p.Status = consts.Idle
if p.listener != nil {
p.listener.Close()
}
close(p.ctlMsgChan)
2016-03-31 18:03:44 +08:00
close(p.workConnChan)
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
}
2016-02-25 10:28:34 +08:00
2016-03-31 18:03:44 +08:00
func (p *ProxyServer) RecvNewWorkConn(c *conn.Conn) {
p.workConnChan <- c
2016-02-25 10:28:34 +08:00
}