GET类型通道创建端点;修改完善返回格式处理逻辑;动态刷新remote令牌

This commit is contained in:
2025-04-02 16:08:55 +08:00
parent 1b8e118fae
commit 13794c2d27
12 changed files with 639 additions and 673 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"log/slog"
"math"
"math/rand/v2"
"platform/pkg/env"
"platform/pkg/orm"
"platform/pkg/rds"
@@ -20,8 +21,6 @@ import (
"time"
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/google/uuid"
"github.com/jxskiss/base62"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
@@ -242,7 +241,7 @@ func (s *channelService) CreateChannel(
authType ChannelAuthType,
count int,
nodeFilter ...NodeFilterConfig,
) ([]string, error) {
) ([]*PortInfo, error) {
var step = time.Now()
var rid = ctx.Value(requestid.ConfigDefault.ContextKey).(string)
@@ -251,7 +250,7 @@ func (s *channelService) CreateChannel(
filter = nodeFilter[0]
}
var addr []string
var addr []*PortInfo
err := q.Q.Transaction(func(tx *q.Query) error {
// 查找套餐
@@ -522,7 +521,7 @@ func assignPort(
authType ChannelAuthType,
expiration time.Time,
filter NodeFilterConfig,
) ([]string, []*models.Channel, error) {
) ([]*PortInfo, []*models.Channel, error) {
var step time.Time
var configs = proxies.configs
@@ -548,7 +547,7 @@ func assignPort(
}
// 配置启用代理
var result []string
var result []*PortInfo
var channels []*models.Channel
for _, config := range configs {
var err error
@@ -595,9 +594,14 @@ func assignPort(
Expiration: expiration,
})
}
result = append(result, &PortInfo{
Proto: string(protocol),
Host: proxy.Host,
Port: port,
})
case ChannelAuthTypePass:
username, password := genPassPair()
configs[i].Whitelist = new([]string)
configs[i].Whitelist = &[]string{}
configs[i].Userpass = v.P(fmt.Sprintf("%s:%s", username, password))
channels = append(channels, &models.Channel{
UserID: userId,
@@ -610,9 +614,14 @@ func assignPort(
Protocol: string(protocol),
Expiration: expiration,
})
result = append(result, &PortInfo{
Proto: string(protocol),
Host: proxy.Host,
Port: port,
Username: &username,
Password: &password,
})
}
result = append(result, fmt.Sprintf("%s://%s:%d", protocol, proxy.Host, port))
}
if len(configs) < count {
@@ -659,20 +668,34 @@ func assignPort(
return result, channels, nil
}
type PortInfo struct {
Proto string `json:"-"`
Host string `json:"host"`
Port int `json:"port"`
Username *string `json:"username,omitempty"`
Password *string `json:"password,omitempty"`
}
// endregion
func genPassPair() (string, string) {
usernameBytes, err := uuid.New().MarshalBinary()
if err != nil {
panic(err)
var letters = []rune("abcdefghjkmnpqrstuvwxyz23456789")
var alphabet = []rune("abcdefghjkmnpqrstuvwxyz")
var numbers = []rune("23456789")
var username = make([]rune, 6)
var password = make([]rune, 6)
for i := range 6 {
if i < 2 {
username[i] = alphabet[rand.N(len(alphabet))]
} else {
username[i] = numbers[rand.N(len(numbers))]
}
password[i] = letters[rand.N(len(letters))]
}
passwordBytes, err := uuid.New().MarshalBinary()
if err != nil {
panic(err)
}
username := base62.EncodeToString(usernameBytes)
password := base62.EncodeToString(passwordBytes)
return username, password
// return string(username), string(password)
return "123123", "123123"
}
func chKey(channel *models.Channel) string {