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.
|
|
|
|
|
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-02-25 10:28:34 +08:00
|
|
|
"time"
|
2016-01-27 21:24:36 +08:00
|
|
|
|
2016-02-25 17:38:34 +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
|
2016-02-19 17:01:47 +08:00
|
|
|
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-03-31 18:03:44 +08:00
|
|
|
p.workConnChan = 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
|
|
|
|
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 {
|
2016-02-19 17:01:47 +08:00
|
|
|
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-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
|
2016-02-19 17:01:47 +08:00
|
|
|
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 {
|
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
|
2016-02-18 11:42:31 +08:00
|
|
|
// 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()
|
2016-02-18 16:56:55 +08:00
|
|
|
p.Status = consts.Idle
|
2016-03-14 00:39:32 +08:00
|
|
|
if p.listener != nil {
|
|
|
|
p.listener.Close()
|
|
|
|
}
|
2016-02-19 17:01:47 +08:00
|
|
|
close(p.ctlMsgChan)
|
2016-03-31 18:03:44 +08:00
|
|
|
close(p.workConnChan)
|
2016-02-19 17:01:47 +08:00
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|