Skip to content

Commit

Permalink
优化端口处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Jul 28, 2020
1 parent f596447 commit f70157d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ClientTCP struct {
}

// WriteClient 生成客户端json
func WriteClient(password string, domain string, writePath string) bool {
func WriteClient(port int, password, domain, writePath string) bool {
box := packr.New("client.json", "../asset")
data, err := box.Find("client.json")
if err != nil {
Expand All @@ -40,6 +40,7 @@ func WriteClient(password string, domain string, writePath string) bool {
return false
}
config.RemoteAddr = domain
config.RemotePort = port
config.Password = []string{password}
outData, err := json.MarshalIndent(config, "", " ")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions trojan/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var clientPath = "/root/config.json"
func GenClientJson() {
fmt.Println()
var user core.User
domain := GetDomain()
domain, port := GetDomainAndPort()
mysql := core.GetMysql()
userList := mysql.GetData()
if userList == nil {
Expand All @@ -35,7 +35,7 @@ func GenClientJson() {
fmt.Println(util.Red("Base64解码失败: " + err.Error()))
return
}
if !core.WriteClient(string(pass), domain, clientPath) {
if !core.WriteClient(port, string(pass), domain, clientPath) {
fmt.Println(util.Red("生成配置文件失败!"))
} else {
fmt.Println("成功生成配置文件: " + util.Green(clientPath))
Expand Down
7 changes: 4 additions & 3 deletions trojan/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ func SetDomain(domain string) {
}
}

// GetDomain 获取域名
func GetDomain() string {
return core.Load("").SSl.Sni
// GetDomainAndPort 获取域名和端口
func GetDomainAndPort() (string, int) {
config := core.Load("")
return config.SSl.Sni, config.LocalPort
}
4 changes: 2 additions & 2 deletions trojan/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func UserList(ids ...string) []*core.User {
fmt.Println("连接mysql失败!")
return nil
}
domain := GetDomain()
domain, port := GetDomainAndPort()
for i, k := range userList {
pass, err := base64.StdEncoding.DecodeString(k.Password)
if err != nil {
Expand All @@ -125,7 +125,7 @@ func UserList(ids ...string) []*core.User {
} else {
fmt.Println("流量限额: " + util.Cyan(util.Bytefmt(uint64(k.Quota))))
}
fmt.Println("分享链接: " + util.Green(fmt.Sprintf("trojan://%s@%s:443", string(pass), domain)))
fmt.Println("分享链接: " + util.Green(fmt.Sprintf("trojan://%s@%s:%d", string(pass), domain, port)))
fmt.Println()
}
return userList
Expand Down
6 changes: 4 additions & 2 deletions web/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ func UserList(findUser string) *ResponseBody {
responseBody.Msg = "连接mysql失败!"
return &responseBody
}
domain := trojan.GetDomain()
domain, port := trojan.GetDomainAndPort()
responseBody.Data = map[string]interface{}{
"domain": domain,
"port": port,
"userList": userList,
}
return &responseBody
Expand All @@ -44,9 +45,10 @@ func PageUserList(curPage int, pageSize int) *ResponseBody {
responseBody.Msg = "连接mysql失败!"
return &responseBody
}
domain := trojan.GetDomain()
domain, port := trojan.GetDomainAndPort()
responseBody.Data = map[string]interface{}{
"domain": domain,
"port": port,
"pageData": pageData,
}
return &responseBody
Expand Down

0 comments on commit f70157d

Please sign in to comment.