mirror of
https://github.com/fatedier/frp.git
synced 2025-07-20 04:39:26 +07:00
First available commit
This commit is contained in:
70
pkg/models/client.go
Normal file
70
pkg/models/client.go
Normal file
@ -0,0 +1,70 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"frp/pkg/utils/conn"
|
||||
"frp/pkg/utils/log"
|
||||
)
|
||||
|
||||
type ProxyClient struct {
|
||||
Name string
|
||||
Passwd string
|
||||
LocalPort int64
|
||||
}
|
||||
|
||||
func (p *ProxyClient) GetLocalConn() (c *conn.Conn, err error) {
|
||||
c = &conn.Conn{}
|
||||
err = c.ConnectServer("127.0.0.1", p.LocalPort)
|
||||
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) {
|
||||
c = &conn.Conn{}
|
||||
defer func(){
|
||||
if err != nil {
|
||||
c.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
err = c.ConnectServer(addr, port)
|
||||
if err != nil {
|
||||
log.Error("ProxyName [%s], connect to server [%s:%d] error, %v", p.Name, addr, port, err)
|
||||
return
|
||||
}
|
||||
|
||||
req := &ClientCtlReq{
|
||||
Type: WorkConn,
|
||||
ProxyName: p.Name,
|
||||
Passwd: p.Passwd,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
log.Debug("Join two conns, (l[%s] r[%s]) (l[%s] r[%s])", localConn.GetLocalAddr(), localConn.GetRemoteAddr(),
|
||||
remoteConn.GetLocalAddr(), remoteConn.GetRemoteAddr())
|
||||
go conn.Join(localConn, remoteConn)
|
||||
return nil
|
||||
}
|
27
pkg/models/msg.go
Normal file
27
pkg/models/msg.go
Normal file
@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
type GeneralRes struct {
|
||||
Code int64 `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
// type
|
||||
const (
|
||||
ControlConn = iota
|
||||
WorkConn
|
||||
)
|
||||
|
||||
type ClientCtlReq struct {
|
||||
Type int64 `json:"type"`
|
||||
ProxyName string `json:"proxy_name"`
|
||||
Passwd string `json:"passwd"`
|
||||
}
|
||||
|
||||
type ClientCtlRes struct {
|
||||
GeneralRes
|
||||
}
|
||||
|
||||
|
||||
type ServerCtlReq struct {
|
||||
Type int64 `json:"type"`
|
||||
}
|
116
pkg/models/server.go
Normal file
116
pkg/models/server.go
Normal file
@ -0,0 +1,116 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"container/list"
|
||||
|
||||
"frp/pkg/utils/conn"
|
||||
"frp/pkg/utils/log"
|
||||
)
|
||||
|
||||
const (
|
||||
Idle = iota
|
||||
Working
|
||||
)
|
||||
|
||||
type ProxyServer struct {
|
||||
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
|
||||
CliConnChan chan *conn.Conn // get client conns from control goroutine
|
||||
UserConnList *list.List // store user conns
|
||||
Mutex sync.Mutex
|
||||
}
|
||||
|
||||
func (p *ProxyServer) Init() {
|
||||
p.Status = Idle
|
||||
p.CtlMsgChan = make(chan int64)
|
||||
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
|
||||
c := p.Listener.GetConn()
|
||||
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()
|
||||
continue
|
||||
}
|
||||
p.Unlock()
|
||||
|
||||
// msg will transfer to another without modifying
|
||||
log.Debug("Join two conns, (l[%s] r[%s]) (l[%s] r[%s])", cliConn.GetLocalAddr(), cliConn.GetRemoteAddr(),
|
||||
userConn.GetLocalAddr(), userConn.GetRemoteAddr())
|
||||
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()
|
||||
}
|
||||
|
||||
func (p *ProxyServer) WaitUserConn() (res int64) {
|
||||
res = <-p.CtlMsgChan
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user