Change directory structure, move models and utils to root directory

This commit is contained in:
fatedier 2016-02-18 16:56:55 +08:00
parent 30da2a2d15
commit 50165053f8
14 changed files with 76 additions and 67 deletions

View File

@ -4,8 +4,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"github.com/fatedier/frp/pkg/models" "github.com/fatedier/frp/models/client"
ini "github.com/vaughan0/go-ini" ini "github.com/vaughan0/go-ini"
) )
@ -19,7 +18,7 @@ var (
HeartBeatInterval int64 = 5 HeartBeatInterval int64 = 5
) )
var ProxyClients map[string]*models.ProxyClient = make(map[string]*models.ProxyClient) var ProxyClients map[string]*client.ProxyClient = make(map[string]*client.ProxyClient)
func LoadConf(confFile string) (err error) { func LoadConf(confFile string) (err error) {
var tmpStr string var tmpStr string
@ -59,7 +58,7 @@ func LoadConf(confFile string) (err error) {
// servers // servers
for name, section := range conf { for name, section := range conf {
if name != "common" { if name != "common" {
proxyClient := &models.ProxyClient{} proxyClient := &client.ProxyClient{}
proxyClient.Name = name proxyClient.Name = name
proxyClient.Passwd, ok = section["passwd"] proxyClient.Passwd, ok = section["passwd"]

View File

@ -6,14 +6,16 @@ import (
"sync" "sync"
"time" "time"
"github.com/fatedier/frp/pkg/models" "github.com/fatedier/frp/models/client"
"github.com/fatedier/frp/pkg/utils/conn" "github.com/fatedier/frp/models/consts"
"github.com/fatedier/frp/pkg/utils/log" "github.com/fatedier/frp/models/msg"
"github.com/fatedier/frp/utils/conn"
"github.com/fatedier/frp/utils/log"
) )
var isHeartBeatContinue bool = true var isHeartBeatContinue bool = true
func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) { func ControlProcess(cli *client.ProxyClient, wait *sync.WaitGroup) {
defer wait.Done() defer wait.Done()
c := loginToServer(cli) c := loginToServer(cli)
@ -54,7 +56,7 @@ func ControlProcess(cli *models.ProxyClient, wait *sync.WaitGroup) {
} }
} }
func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) { func loginToServer(cli *client.ProxyClient) (connection *conn.Conn) {
c := &conn.Conn{} c := &conn.Conn{}
connection = nil connection = nil
@ -65,8 +67,8 @@ func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) {
break break
} }
req := &models.ClientCtlReq{ req := &msg.ClientCtlReq{
Type: models.ControlConn, Type: consts.CtlConn,
ProxyName: cli.Name, ProxyName: cli.Name,
Passwd: cli.Passwd, Passwd: cli.Passwd,
} }
@ -84,7 +86,7 @@ func loginToServer(cli *models.ProxyClient) (connection *conn.Conn) {
} }
log.Debug("ProxyName [%s], read [%s]", cli.Name, res) log.Debug("ProxyName [%s], read [%s]", cli.Name, res)
clientCtlRes := &models.ClientCtlRes{} clientCtlRes := &msg.ClientCtlRes{}
if err = json.Unmarshal([]byte(res), &clientCtlRes); err != nil { if err = json.Unmarshal([]byte(res), &clientCtlRes); err != nil {
log.Error("ProxyName [%s], format server response error, %v", cli.Name, err) log.Error("ProxyName [%s], format server response error, %v", cli.Name, err)
break break

View File

@ -4,7 +4,7 @@ import (
"os" "os"
"sync" "sync"
"github.com/fatedier/frp/pkg/utils/log" "github.com/fatedier/frp/utils/log"
) )
func main() { func main() {

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"github.com/fatedier/frp/pkg/models" "github.com/fatedier/frp/models/server"
ini "github.com/vaughan0/go-ini" ini "github.com/vaughan0/go-ini"
) )
@ -19,7 +19,7 @@ var (
HeartBeatTimeout int64 = 30 HeartBeatTimeout int64 = 30
) )
var ProxyServers map[string]*models.ProxyServer = make(map[string]*models.ProxyServer) var ProxyServers map[string]*server.ProxyServer = make(map[string]*server.ProxyServer)
func LoadConf(confFile string) (err error) { func LoadConf(confFile string) (err error) {
var tmpStr string var tmpStr string
@ -59,7 +59,7 @@ func LoadConf(confFile string) (err error) {
// servers // servers
for name, section := range conf { for name, section := range conf {
if name != "common" { if name != "common" {
proxyServer := &models.ProxyServer{} proxyServer := &server.ProxyServer{}
proxyServer.Name = name proxyServer.Name = name
proxyServer.Passwd, ok = section["passwd"] proxyServer.Passwd, ok = section["passwd"]

View File

@ -6,9 +6,11 @@ import (
"io" "io"
"time" "time"
"github.com/fatedier/frp/pkg/models" "github.com/fatedier/frp/models/consts"
"github.com/fatedier/frp/pkg/utils/conn" "github.com/fatedier/frp/models/msg"
"github.com/fatedier/frp/pkg/utils/log" "github.com/fatedier/frp/models/server"
"github.com/fatedier/frp/utils/conn"
"github.com/fatedier/frp/utils/log"
) )
func ProcessControlConn(l *conn.Listener) { func ProcessControlConn(l *conn.Listener) {
@ -30,18 +32,18 @@ func controlWorker(c *conn.Conn) {
} }
log.Debug("get: %s", res) log.Debug("get: %s", res)
clientCtlReq := &models.ClientCtlReq{} clientCtlReq := &msg.ClientCtlReq{}
clientCtlRes := &models.ClientCtlRes{} clientCtlRes := &msg.ClientCtlRes{}
if err := json.Unmarshal([]byte(res), &clientCtlReq); err != nil { if err := json.Unmarshal([]byte(res), &clientCtlReq); err != nil {
log.Warn("Parse err: %v : %s", err, res) log.Warn("Parse err: %v : %s", err, res)
return return
} }
// check // check
succ, msg, needRes := checkProxy(clientCtlReq, c) succ, info, needRes := checkProxy(clientCtlReq, c)
if !succ { if !succ {
clientCtlRes.Code = 1 clientCtlRes.Code = 1
clientCtlRes.Msg = msg clientCtlRes.Msg = info
} }
if needRes { if needRes {
@ -70,8 +72,8 @@ func controlWorker(c *conn.Conn) {
// read control msg from client // read control msg from client
go readControlMsgFromClient(server, c) go readControlMsgFromClient(server, c)
serverCtlReq := &models.ClientCtlReq{} serverCtlReq := &msg.ClientCtlReq{}
serverCtlReq.Type = models.WorkConn serverCtlReq.Type = consts.WorkConn
for { for {
_, isStop := server.WaitUserConn() _, isStop := server.WaitUserConn()
if isStop { if isStop {
@ -92,52 +94,53 @@ func controlWorker(c *conn.Conn) {
return return
} }
func checkProxy(req *models.ClientCtlReq, c *conn.Conn) (succ bool, msg string, needRes bool) { func checkProxy(req *msg.ClientCtlReq, c *conn.Conn) (succ bool, info string, needRes bool) {
succ = false succ = false
needRes = true needRes = true
// check if proxy name exist // check if proxy name exist
server, ok := ProxyServers[req.ProxyName] server, ok := ProxyServers[req.ProxyName]
if !ok { if !ok {
msg = fmt.Sprintf("ProxyName [%s] is not exist", req.ProxyName) info = fmt.Sprintf("ProxyName [%s] is not exist", req.ProxyName)
log.Warn(msg) log.Warn(info)
return return
} }
// check password // check password
if req.Passwd != server.Passwd { if req.Passwd != server.Passwd {
msg = fmt.Sprintf("ProxyName [%s], password is not correct", req.ProxyName) info = fmt.Sprintf("ProxyName [%s], password is not correct", req.ProxyName)
log.Warn(msg) log.Warn(info)
return return
} }
// control conn // control conn
if req.Type == models.ControlConn { if req.Type == consts.CtlConn {
if server.Status != models.Idle { if server.Status != consts.Idle {
msg = fmt.Sprintf("ProxyName [%s], already in use", req.ProxyName) info = fmt.Sprintf("ProxyName [%s], already in use", req.ProxyName)
log.Warn(msg) log.Warn(info)
return return
} }
// start proxy and listen for user conn, no block // start proxy and listen for user conn, no block
err := server.Start() err := server.Start()
if err != nil { if err != nil {
msg = fmt.Sprintf("ProxyName [%s], start proxy error: %v", req.ProxyName, err.Error()) info = fmt.Sprintf("ProxyName [%s], start proxy error: %v", req.ProxyName, err.Error())
log.Warn(msg) log.Warn(info)
return return
} }
log.Info("ProxyName [%s], start proxy success", req.ProxyName) log.Info("ProxyName [%s], start proxy success", req.ProxyName)
} else if req.Type == models.WorkConn { } else if req.Type == consts.WorkConn {
// work conn // work conn
needRes = false needRes = false
if server.Status != models.Working { if server.Status != consts.Working {
log.Warn("ProxyName [%s], is not working when it gets one new work conn", req.ProxyName) log.Warn("ProxyName [%s], is not working when it gets one new work conn", req.ProxyName)
return return
} }
server.CliConnChan <- c server.CliConnChan <- c
} else { } else {
log.Warn("ProxyName [%s], type [%d] unsupport", req.ProxyName, req.Type) info = fmt.Sprintf("ProxyName [%s], type [%d] unsupport", req.ProxyName, req.Type)
log.Warn(info)
return return
} }
@ -145,7 +148,7 @@ func checkProxy(req *models.ClientCtlReq, c *conn.Conn) (succ bool, msg string,
return return
} }
func readControlMsgFromClient(server *models.ProxyServer, c *conn.Conn) { func readControlMsgFromClient(server *server.ProxyServer, c *conn.Conn) {
isContinueRead := true isContinueRead := true
f := func() { f := func() {
isContinueRead = false isContinueRead = false

View File

@ -3,8 +3,8 @@ package main
import ( import (
"os" "os"
"github.com/fatedier/frp/pkg/utils/conn" "github.com/fatedier/frp/utils/conn"
"github.com/fatedier/frp/pkg/utils/log" "github.com/fatedier/frp/utils/log"
) )
func main() { func main() {

View File

@ -1,10 +1,12 @@
package models package client
import ( import (
"encoding/json" "encoding/json"
"github.com/fatedier/frp/pkg/utils/conn" "github.com/fatedier/frp/models/consts"
"github.com/fatedier/frp/pkg/utils/log" "github.com/fatedier/frp/models/msg"
"github.com/fatedier/frp/utils/conn"
"github.com/fatedier/frp/utils/log"
) )
type ProxyClient struct { type ProxyClient struct {
@ -36,8 +38,8 @@ func (p *ProxyClient) GetRemoteConn(addr string, port int64) (c *conn.Conn, err
return return
} }
req := &ClientCtlReq{ req := &msg.ClientCtlReq{
Type: WorkConn, Type: consts.WorkConn,
ProxyName: p.Name, ProxyName: p.Name,
Passwd: p.Passwd, Passwd: p.Passwd,
} }

13
models/consts/consts.go Normal file
View File

@ -0,0 +1,13 @@
package consts
// server status
const (
Idle = iota
Working
)
// connection type
const (
CtlConn = iota
WorkConn
)

View File

@ -1,16 +1,10 @@
package models package msg
type GeneralRes struct { type GeneralRes struct {
Code int64 `json:"code"` Code int64 `json:"code"`
Msg string `json:"msg"` Msg string `json:"msg"`
} }
// type
const (
ControlConn = iota
WorkConn
)
type ClientCtlReq struct { type ClientCtlReq struct {
Type int64 `json:"type"` Type int64 `json:"type"`
ProxyName string `json:"proxy_name"` ProxyName string `json:"proxy_name"`

View File

@ -1,16 +1,12 @@
package models package server
import ( import (
"container/list" "container/list"
"sync" "sync"
"github.com/fatedier/frp/pkg/utils/conn" "github.com/fatedier/frp/models/consts"
"github.com/fatedier/frp/pkg/utils/log" "github.com/fatedier/frp/utils/conn"
) "github.com/fatedier/frp/utils/log"
const (
Idle = iota
Working
) )
type ProxyServer struct { type ProxyServer struct {
@ -29,7 +25,7 @@ type ProxyServer struct {
} }
func (p *ProxyServer) Init() { func (p *ProxyServer) Init() {
p.Status = Idle p.Status = consts.Idle
p.CtlMsgChan = make(chan int64) p.CtlMsgChan = make(chan int64)
p.StopBlockChan = make(chan int64) p.StopBlockChan = make(chan int64)
p.CliConnChan = make(chan *conn.Conn) p.CliConnChan = make(chan *conn.Conn)
@ -51,7 +47,7 @@ func (p *ProxyServer) Start() (err error) {
return err return err
} }
p.Status = Working p.Status = consts.Working
// start a goroutine for listener // start a goroutine for listener
go func() { go func() {
@ -62,7 +58,7 @@ func (p *ProxyServer) Start() (err error) {
// put to list // put to list
p.Lock() p.Lock()
if p.Status != Working { if p.Status != consts.Working {
log.Debug("ProxyName [%s] is not working, new user conn close", p.Name) log.Debug("ProxyName [%s] is not working, new user conn close", p.Name)
c.Close() c.Close()
p.Unlock() p.Unlock()
@ -107,7 +103,7 @@ func (p *ProxyServer) Start() (err error) {
func (p *ProxyServer) Close() { func (p *ProxyServer) Close() {
p.Lock() p.Lock()
p.Status = Idle p.Status = consts.Idle
p.CtlMsgChan = make(chan int64) p.CtlMsgChan = make(chan int64)
p.CliConnChan = make(chan *conn.Conn) p.CliConnChan = make(chan *conn.Conn)
p.UserConnList = list.New() p.UserConnList = list.New()

View File

@ -7,7 +7,7 @@ import (
"net" "net"
"sync" "sync"
"github.com/fatedier/frp/pkg/utils/log" "github.com/fatedier/frp/utils/log"
) )
type Listener struct { type Listener struct {