mirror of
https://gitee.com/konyshe/goodlink.git
synced 2025-09-26 12:41:14 +08:00
优化代码
This commit is contained in:
14
pro/comm.go
14
pro/comm.go
@@ -141,9 +141,19 @@ func RedisDel() {
|
||||
m_redis_db.Del(m_md5_tun_key)
|
||||
}
|
||||
|
||||
func GetUDPLocalIPPort(level string) (string, int) {
|
||||
conn, err := net.Dial(level, "ifconfig.co:80")
|
||||
if err != nil {
|
||||
return "", 0
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
return conn.LocalAddr().(*net.UDPAddr).IP.String(), conn.LocalAddr().(*net.UDPAddr).Port
|
||||
}
|
||||
|
||||
func GetUDPAddr() (conn *net.UDPConn, addr tun.AddrType) {
|
||||
addr.LocalIPv4, _ = utils.GetUDPLocalIPPort("udp4")
|
||||
addr.IPv6, _ = utils.GetUDPLocalIPPort("udp6")
|
||||
addr.LocalIPv4, _ = GetUDPLocalIPPort("udp4")
|
||||
addr.IPv6, _ = GetUDPLocalIPPort("udp6")
|
||||
|
||||
var err error
|
||||
|
||||
|
@@ -23,7 +23,7 @@ func GetLocalQuicConn(conn *net.UDPConn, addr *tun.AddrType, count int) (*tun.Tu
|
||||
var tun_active *tun.TunActive
|
||||
var tun_passive *tun.TunPassive
|
||||
|
||||
SessionID := string(utils.RandomBytes(24))
|
||||
SessionID := string(go2.RandomBytes(24))
|
||||
utils.Log().DebugF("会话ID: %s", SessionID)
|
||||
|
||||
redisJson := RedisJsonType{
|
||||
|
@@ -10,7 +10,6 @@ import (
|
||||
"goodlink/config"
|
||||
"goodlink/pro"
|
||||
_ "goodlink/pro"
|
||||
"goodlink/utils"
|
||||
|
||||
_ "embed"
|
||||
_ "net/http/pprof"
|
||||
@@ -47,7 +46,7 @@ func GetMainUI(myWindow *fyne.Window) *fyne.Container {
|
||||
}
|
||||
|
||||
m_button_key_create = widget.NewButton("生成密钥", func() {
|
||||
m_validated_key.SetText(string(utils.RandomBytes(24)))
|
||||
m_validated_key.SetText(string(go2.RandomBytes(24)))
|
||||
})
|
||||
key_copy_button := widget.NewButton("复制密钥", func() {
|
||||
clipboard.WriteAll(m_validated_key.Text)
|
||||
|
51
utils/net.go
51
utils/net.go
@@ -1,51 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetUDPLocalIPPort(level string) (string, int) {
|
||||
conn, err := net.Dial(level, "ifconfig.co:80")
|
||||
if err != nil {
|
||||
return "", 0
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
return conn.LocalAddr().(*net.UDPAddr).IP.String(), conn.LocalAddr().(*net.UDPAddr).Port
|
||||
}
|
||||
|
||||
func GetTCPWanIPv4Port(dialer *net.Dialer) (string, int) {
|
||||
var res []byte
|
||||
var err error
|
||||
var resp *http.Response
|
||||
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: dialer.DialContext,
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true, // 跳过证书验证
|
||||
},
|
||||
},
|
||||
Timeout: 3 * time.Second,
|
||||
}
|
||||
if resp, err = client.Get("https://goodlink.kony.vip/ip"); err != nil {
|
||||
return "", 0
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if res, err = io.ReadAll(resp.Body); err != nil {
|
||||
return "", 0
|
||||
}
|
||||
|
||||
localAddr := string(res)
|
||||
|
||||
ip := strings.Split(localAddr, ":")[0]
|
||||
port, _ := strconv.Atoi(strings.Split(localAddr, ":")[1])
|
||||
return ip, port
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
func RandomBytes(length int) []byte {
|
||||
bytes := make([]byte, length)
|
||||
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
for i := 0; i < length; {
|
||||
bint, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
bytes[i] = charset[bint.Int64()]
|
||||
i++
|
||||
}
|
||||
return bytes
|
||||
}
|
Reference in New Issue
Block a user