package SunnyProxy

import (
	"bufio"
	"strconv"
	"sync"
)

var IsRun = false //运行状态
var RunErr error  //运行错误信息

// Stat
//
// 启动.可以指定CA 证书 如果留空 则使用默认的证书
//
// prot 端口号
//
// certCa crt证书
//
// certKey 证书Key
var proxyX *Proxy
var OpenTcpcallback bool
var GlobalLock sync.Mutex
var GlobalProxyIp = ""
var GlobalProxyAuth = ""
var GlobalProxyProxyTimeout = 30
var MustTcp bool
var VerifyUser bool
var VerifyUserList = make(map[string]string)
var VerifyUserLock = sync.Mutex{}

var TcpSceneLock sync.Mutex
var TcpScenes = make(map[int]interface{})
var InstallCa string

func Stat(prot int, delegate Delegate, certCa, certKey string, OpenTcpx int) {
	OpenTcpcallback = OpenTcpx != 0
	var certCa_ = certCa
	var certKey_ = certKey
	if certCa_ == "" {
		certCa_ = RootCa
		certKey_ = RootKey
	}

	InstallCa = certCa_
	proxyX = NewWithDelegate(delegate, certCa_, certKey_)
	IsRun = true
	StartS5(strconv.Itoa(prot))
	IsRun = false
}

//Stop
//
//停止运行.
func Stop() {
	if S5Server != nil && S5Server.listen != nil && (*S5Server.listen) != nil {
		RunErr = (*S5Server.listen).Close()
	}
	CloseAllTCP()
}

var TcpSendLock sync.Mutex
var TcpRceLock sync.Mutex

type Brw struct {
	C *bufio.Writer
	B *bufio.Writer
	L sync.Mutex
}
