2016-03-14 03:18:24 +00: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 08:56:55 +00:00
|
|
|
package client
|
2016-01-27 13:24:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-04-05 09:18:21 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2016-01-27 13:24:36 +00:00
|
|
|
|
2016-06-24 05:12:34 +00:00
|
|
|
"frp/models/config"
|
2016-02-25 09:38:34 +00:00
|
|
|
"frp/models/consts"
|
|
|
|
"frp/models/msg"
|
|
|
|
"frp/utils/conn"
|
|
|
|
"frp/utils/log"
|
2016-04-05 09:18:21 +00:00
|
|
|
"frp/utils/pcrypto"
|
2016-01-27 13:24:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProxyClient struct {
|
2016-06-24 05:12:34 +00:00
|
|
|
config.BaseConf
|
|
|
|
LocalIp string
|
|
|
|
LocalPort int64
|
2016-01-27 13:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProxyClient) GetLocalConn() (c *conn.Conn, err error) {
|
2016-02-29 04:38:45 +00:00
|
|
|
c, err = conn.ConnectServer(p.LocalIp, p.LocalPort)
|
2016-01-27 13:24:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("ProxyName [%s], connect to local port error, %v", p.Name, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProxyClient) GetRemoteConn(addr string, port int64) (c *conn.Conn, err error) {
|
2016-02-03 10:46:24 +00:00
|
|
|
defer func() {
|
2016-01-27 13:24:36 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-02-19 09:01:47 +00:00
|
|
|
c, err = conn.ConnectServer(addr, port)
|
2016-01-27 13:24:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("ProxyName [%s], connect to server [%s:%d] error, %v", p.Name, addr, port, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-05 09:18:21 +00:00
|
|
|
nowTime := time.Now().Unix()
|
|
|
|
authKey := pcrypto.GetAuthKey(p.Name + p.AuthToken + fmt.Sprintf("%d", nowTime))
|
2016-03-31 10:03:44 +00:00
|
|
|
req := &msg.ControlReq{
|
|
|
|
Type: consts.NewWorkConn,
|
2016-02-03 10:46:24 +00:00
|
|
|
ProxyName: p.Name,
|
2016-04-05 09:18:21 +00:00
|
|
|
AuthKey: authKey,
|
|
|
|
Timestamp: nowTime,
|
2016-01-27 13:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf, _ := json.Marshal(req)
|
|
|
|
err = c.Write(string(buf) + "\n")
|
|
|
|
if err != nil {
|
|
|
|
log.Error("ProxyName [%s], write to server error, %v", p.Name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProxyClient) StartTunnel(serverAddr string, serverPort int64) (err error) {
|
|
|
|
localConn, err := p.GetLocalConn()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
remoteConn, err := p.GetRemoteConn(serverAddr, serverPort)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-18 03:42:31 +00:00
|
|
|
// l means local, r means remote
|
2016-03-31 10:03:44 +00:00
|
|
|
log.Debug("Join two connections, (l[%s] r[%s]) (l[%s] r[%s])", localConn.GetLocalAddr(), localConn.GetRemoteAddr(),
|
2016-02-03 10:46:24 +00:00
|
|
|
remoteConn.GetLocalAddr(), remoteConn.GetRemoteAddr())
|
2016-06-24 05:12:34 +00:00
|
|
|
go msg.JoinMore(localConn, remoteConn, p.BaseConf)
|
2016-03-22 09:18:05 +00:00
|
|
|
|
2016-01-27 13:24:36 +00:00
|
|
|
return nil
|
|
|
|
}
|