mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-06-17 12:43:18 +08:00
Refactor error handling
This commit is contained in:
@ -2,9 +2,9 @@ package database
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/bestnite/sub2clash/common"
|
||||||
"github.com/bestnite/sub2clash/model"
|
"github.com/bestnite/sub2clash/model"
|
||||||
|
|
||||||
"go.etcd.io/bbolt"
|
"go.etcd.io/bbolt"
|
||||||
@ -17,13 +17,16 @@ func ConnectDB() error {
|
|||||||
|
|
||||||
db, err := bbolt.Open(path, 0600, nil)
|
db, err := bbolt.Open(path, 0600, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return common.NewDatabaseConnectError(err)
|
||||||
}
|
}
|
||||||
DB = db
|
DB = db
|
||||||
|
|
||||||
return db.Update(func(tx *bbolt.Tx) error {
|
return db.Update(func(tx *bbolt.Tx) error {
|
||||||
_, err := tx.CreateBucketIfNotExists([]byte("ShortLinks"))
|
_, err := tx.CreateBucketIfNotExists([]byte("ShortLinks"))
|
||||||
return err
|
if err != nil {
|
||||||
|
return common.NewDatabaseConnectError(err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +36,7 @@ func FindShortLinkByHash(hash string) (*model.ShortLink, error) {
|
|||||||
b := tx.Bucket([]byte("ShortLinks"))
|
b := tx.Bucket([]byte("ShortLinks"))
|
||||||
v := b.Get([]byte(hash))
|
v := b.Get([]byte(hash))
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return errors.New("ShortLink not found")
|
return common.NewRecordNotFoundError("ShortLink", hash)
|
||||||
}
|
}
|
||||||
return json.Unmarshal(v, &shortLink)
|
return json.Unmarshal(v, &shortLink)
|
||||||
})
|
})
|
||||||
|
192
common/errors.go
Normal file
192
common/errors.go
Normal file
@ -0,0 +1,192 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CommonError represents a structured error type for the common package
|
||||||
|
type CommonError struct {
|
||||||
|
Code ErrorCode
|
||||||
|
Message string
|
||||||
|
Cause error
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrorCode represents different types of errors
|
||||||
|
type ErrorCode string
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Directory operation errors
|
||||||
|
ErrDirCreation ErrorCode = "DIRECTORY_CREATION_FAILED"
|
||||||
|
ErrDirAccess ErrorCode = "DIRECTORY_ACCESS_FAILED"
|
||||||
|
|
||||||
|
// File operation errors
|
||||||
|
ErrFileNotFound ErrorCode = "FILE_NOT_FOUND"
|
||||||
|
ErrFileRead ErrorCode = "FILE_READ_FAILED"
|
||||||
|
ErrFileWrite ErrorCode = "FILE_WRITE_FAILED"
|
||||||
|
ErrFileCreate ErrorCode = "FILE_CREATE_FAILED"
|
||||||
|
|
||||||
|
// Network operation errors
|
||||||
|
ErrNetworkRequest ErrorCode = "NETWORK_REQUEST_FAILED"
|
||||||
|
ErrNetworkResponse ErrorCode = "NETWORK_RESPONSE_FAILED"
|
||||||
|
|
||||||
|
// Template and configuration errors
|
||||||
|
ErrTemplateLoad ErrorCode = "TEMPLATE_LOAD_FAILED"
|
||||||
|
ErrTemplateParse ErrorCode = "TEMPLATE_PARSE_FAILED"
|
||||||
|
ErrConfigInvalid ErrorCode = "CONFIG_INVALID"
|
||||||
|
|
||||||
|
// Subscription errors
|
||||||
|
ErrSubscriptionLoad ErrorCode = "SUBSCRIPTION_LOAD_FAILED"
|
||||||
|
ErrSubscriptionParse ErrorCode = "SUBSCRIPTION_PARSE_FAILED"
|
||||||
|
|
||||||
|
// Regex errors
|
||||||
|
ErrRegexCompile ErrorCode = "REGEX_COMPILE_FAILED"
|
||||||
|
ErrRegexInvalid ErrorCode = "REGEX_INVALID"
|
||||||
|
|
||||||
|
// Database errors
|
||||||
|
ErrDatabaseConnect ErrorCode = "DATABASE_CONNECTION_FAILED"
|
||||||
|
ErrDatabaseQuery ErrorCode = "DATABASE_QUERY_FAILED"
|
||||||
|
ErrRecordNotFound ErrorCode = "RECORD_NOT_FOUND"
|
||||||
|
|
||||||
|
// Validation errors
|
||||||
|
ErrValidation ErrorCode = "VALIDATION_FAILED"
|
||||||
|
ErrInvalidInput ErrorCode = "INVALID_INPUT"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Error returns the string representation of the error
|
||||||
|
func (e *CommonError) Error() string {
|
||||||
|
if e.Cause != nil {
|
||||||
|
return fmt.Sprintf("[%s] %s: %v", e.Code, e.Message, e.Cause)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("[%s] %s", e.Code, e.Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unwrap returns the underlying error
|
||||||
|
func (e *CommonError) Unwrap() error {
|
||||||
|
return e.Cause
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is allows error comparison
|
||||||
|
func (e *CommonError) Is(target error) bool {
|
||||||
|
if t, ok := target.(*CommonError); ok {
|
||||||
|
return e.Code == t.Code
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewError creates a new CommonError
|
||||||
|
func NewError(code ErrorCode, message string, cause error) *CommonError {
|
||||||
|
return &CommonError{
|
||||||
|
Code: code,
|
||||||
|
Message: message,
|
||||||
|
Cause: cause,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSimpleError creates a new CommonError without a cause
|
||||||
|
func NewSimpleError(code ErrorCode, message string) *CommonError {
|
||||||
|
return &CommonError{
|
||||||
|
Code: code,
|
||||||
|
Message: message,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convenience constructors for common error types
|
||||||
|
|
||||||
|
// Directory errors
|
||||||
|
func NewDirCreationError(dirPath string, cause error) *CommonError {
|
||||||
|
return NewError(ErrDirCreation, fmt.Sprintf("failed to create directory: %s", dirPath), cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDirAccessError(dirPath string, cause error) *CommonError {
|
||||||
|
return NewError(ErrDirAccess, fmt.Sprintf("failed to access directory: %s", dirPath), cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
// File errors
|
||||||
|
func NewFileNotFoundError(filePath string) *CommonError {
|
||||||
|
return NewSimpleError(ErrFileNotFound, fmt.Sprintf("file not found: %s", filePath))
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFileReadError(filePath string, cause error) *CommonError {
|
||||||
|
return NewError(ErrFileRead, fmt.Sprintf("failed to read file: %s", filePath), cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFileWriteError(filePath string, cause error) *CommonError {
|
||||||
|
return NewError(ErrFileWrite, fmt.Sprintf("failed to write file: %s", filePath), cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFileCreateError(filePath string, cause error) *CommonError {
|
||||||
|
return NewError(ErrFileCreate, fmt.Sprintf("failed to create file: %s", filePath), cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Network errors
|
||||||
|
func NewNetworkRequestError(url string, cause error) *CommonError {
|
||||||
|
return NewError(ErrNetworkRequest, fmt.Sprintf("network request failed for URL: %s", url), cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNetworkResponseError(message string, cause error) *CommonError {
|
||||||
|
return NewError(ErrNetworkResponse, message, cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Template errors
|
||||||
|
func NewTemplateLoadError(template string, cause error) *CommonError {
|
||||||
|
return NewError(ErrTemplateLoad, fmt.Sprintf("failed to load template: %s", template), cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTemplateParseError(cause error) *CommonError {
|
||||||
|
return NewError(ErrTemplateParse, "failed to parse template", cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscription errors
|
||||||
|
func NewSubscriptionLoadError(url string, cause error) *CommonError {
|
||||||
|
return NewError(ErrSubscriptionLoad, fmt.Sprintf("failed to load subscription: %s", url), cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSubscriptionParseError(cause error) *CommonError {
|
||||||
|
return NewError(ErrSubscriptionParse, "failed to parse subscription", cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Regex errors
|
||||||
|
func NewRegexCompileError(pattern string, cause error) *CommonError {
|
||||||
|
return NewError(ErrRegexCompile, fmt.Sprintf("failed to compile regex pattern: %s", pattern), cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRegexInvalidError(paramName string, cause error) *CommonError {
|
||||||
|
return NewError(ErrRegexInvalid, fmt.Sprintf("invalid regex in parameter: %s", paramName), cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Database errors
|
||||||
|
func NewDatabaseConnectError(cause error) *CommonError {
|
||||||
|
return NewError(ErrDatabaseConnect, "failed to connect to database", cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRecordNotFoundError(recordType string, id string) *CommonError {
|
||||||
|
return NewSimpleError(ErrRecordNotFound, fmt.Sprintf("%s not found: %s", recordType, id))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validation errors
|
||||||
|
func NewValidationError(field string, message string) *CommonError {
|
||||||
|
return NewSimpleError(ErrValidation, fmt.Sprintf("validation failed for %s: %s", field, message))
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInvalidInputError(paramName string, value string) *CommonError {
|
||||||
|
return NewSimpleError(ErrInvalidInput, fmt.Sprintf("invalid input for parameter %s: %s", paramName, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsErrorCode checks if an error has a specific error code
|
||||||
|
func IsErrorCode(err error, code ErrorCode) bool {
|
||||||
|
var commonErr *CommonError
|
||||||
|
if errors.As(err, &commonErr) {
|
||||||
|
return commonErr.Code == code
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetErrorCode extracts the error code from an error
|
||||||
|
func GetErrorCode(err error) (ErrorCode, bool) {
|
||||||
|
var commonErr *CommonError
|
||||||
|
if errors.As(err, &commonErr) {
|
||||||
|
return commonErr.Code, true
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,13 +17,13 @@ func MKDir(dir string) error {
|
|||||||
|
|
||||||
func MkEssentialDir() error {
|
func MkEssentialDir() error {
|
||||||
if err := MKDir("subs"); err != nil {
|
if err := MKDir("subs"); err != nil {
|
||||||
return errors.New("create subs dir failed" + err.Error())
|
return NewDirCreationError("subs", err)
|
||||||
}
|
}
|
||||||
if err := MKDir("logs"); err != nil {
|
if err := MKDir("logs"); err != nil {
|
||||||
return errors.New("create logs dir failed" + err.Error())
|
return NewDirCreationError("logs", err)
|
||||||
}
|
}
|
||||||
if err := MKDir("data"); err != nil {
|
if err := MKDir("data"); err != nil {
|
||||||
return errors.New("create data dir failed" + err.Error())
|
return NewDirCreationError("data", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package common
|
|||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -110,26 +109,26 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
|
|||||||
logger.Logger.Debug(
|
logger.Logger.Debug(
|
||||||
"load template failed", zap.String("template", template), zap.Error(err),
|
"load template failed", zap.String("template", template), zap.Error(err),
|
||||||
)
|
)
|
||||||
return nil, errors.New("加载模板失败: " + err.Error())
|
return nil, NewTemplateLoadError(template, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
unescape, err := url.QueryUnescape(template)
|
unescape, err := url.QueryUnescape(template)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("加载模板失败: " + err.Error())
|
return nil, NewTemplateLoadError(template, err)
|
||||||
}
|
}
|
||||||
templateBytes, err = LoadTemplate(unescape)
|
templateBytes, err = LoadTemplate(unescape)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Debug(
|
logger.Logger.Debug(
|
||||||
"load template failed", zap.String("template", template), zap.Error(err),
|
"load template failed", zap.String("template", template), zap.Error(err),
|
||||||
)
|
)
|
||||||
return nil, errors.New("加载模板失败: " + err.Error())
|
return nil, NewTemplateLoadError(unescape, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = yaml.Unmarshal(templateBytes, &temp)
|
err = yaml.Unmarshal(templateBytes, &temp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Debug("parse template failed", zap.Error(err))
|
logger.Logger.Debug("parse template failed", zap.Error(err))
|
||||||
return nil, errors.New("解析模板失败: " + err.Error())
|
return nil, NewTemplateParseError(err)
|
||||||
}
|
}
|
||||||
var proxyList []P.Proxy
|
var proxyList []P.Proxy
|
||||||
|
|
||||||
@ -143,7 +142,7 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
|
|||||||
logger.Logger.Debug(
|
logger.Logger.Debug(
|
||||||
"load subscription failed", zap.String("url", query.Subs[i]), zap.Error(err),
|
"load subscription failed", zap.String("url", query.Subs[i]), zap.Error(err),
|
||||||
)
|
)
|
||||||
return nil, errors.New("加载订阅失败: " + err.Error())
|
return nil, NewSubscriptionLoadError(query.Subs[i], err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = yaml.Unmarshal(data, &sub)
|
err = yaml.Unmarshal(data, &sub)
|
||||||
@ -161,7 +160,7 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
|
|||||||
zap.String("data", string(data)),
|
zap.String("data", string(data)),
|
||||||
zap.Error(err),
|
zap.Error(err),
|
||||||
)
|
)
|
||||||
return nil, errors.New("加载订阅失败: " + err.Error())
|
return nil, NewSubscriptionParseError(err)
|
||||||
}
|
}
|
||||||
p := parser.ParseProxies(strings.Split(base64, "\n")...)
|
p := parser.ParseProxies(strings.Split(base64, "\n")...)
|
||||||
newProxies = p
|
newProxies = p
|
||||||
@ -193,7 +192,7 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
|
|||||||
yamlBytes, err := yaml.Marshal(proxyList[i])
|
yamlBytes, err := yaml.Marshal(proxyList[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Debug("marshal proxy failed", zap.Error(err))
|
logger.Logger.Debug("marshal proxy failed", zap.Error(err))
|
||||||
return nil, errors.New("marshal proxy failed: " + err.Error())
|
return nil, fmt.Errorf("marshal proxy failed: %w", err)
|
||||||
}
|
}
|
||||||
key := string(yamlBytes)
|
key := string(yamlBytes)
|
||||||
if _, exist := proxies[key]; !exist {
|
if _, exist := proxies[key]; !exist {
|
||||||
@ -209,7 +208,7 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
|
|||||||
removeReg, err := regexp.Compile(query.Remove)
|
removeReg, err := regexp.Compile(query.Remove)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Debug("remove regexp compile failed", zap.Error(err))
|
logger.Logger.Debug("remove regexp compile failed", zap.Error(err))
|
||||||
return nil, errors.New("remove 参数非法: " + err.Error())
|
return nil, NewRegexInvalidError("remove", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if removeReg.MatchString(proxyList[i].Name) {
|
if removeReg.MatchString(proxyList[i].Name) {
|
||||||
@ -227,7 +226,7 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
|
|||||||
replaceReg, err := regexp.Compile(v)
|
replaceReg, err := regexp.Compile(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Debug("replace regexp compile failed", zap.Error(err))
|
logger.Logger.Debug("replace regexp compile failed", zap.Error(err))
|
||||||
return nil, errors.New("replace 参数非法: " + err.Error())
|
return nil, NewRegexInvalidError("replace", err)
|
||||||
}
|
}
|
||||||
replaceRegs = append(replaceRegs, replaceReg)
|
replaceRegs = append(replaceRegs, replaceReg)
|
||||||
}
|
}
|
||||||
@ -312,15 +311,15 @@ func FetchSubscriptionUserInfo(url string, userAgent string, retryTimes int) (st
|
|||||||
resp, err := client.R().SetHeader("User-Agent", userAgent).Head(url)
|
resp, err := client.R().SetHeader("User-Agent", userAgent).Head(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Debug("创建 HEAD 请求失败", zap.Error(err))
|
logger.Logger.Debug("创建 HEAD 请求失败", zap.Error(err))
|
||||||
return "", err
|
return "", NewNetworkRequestError(url, err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
if userInfo := resp.Header().Get("subscription-userinfo"); userInfo != "" {
|
if userInfo := resp.Header().Get("subscription-userinfo"); userInfo != "" {
|
||||||
return userInfo, nil
|
return userInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Logger.Debug("目标 URL 未返回 subscription-userinfo 头", zap.Error(err))
|
logger.Logger.Debug("subscription-userinfo header not found in response")
|
||||||
return "", err
|
return "", NewNetworkResponseError("subscription-userinfo header not found", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MergeSubAndTemplate(temp *model.Subscription, sub *model.Subscription, igcg bool) {
|
func MergeSubAndTemplate(temp *model.Subscription, sub *model.Subscription, igcg bool) {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@ -23,5 +22,5 @@ func LoadTemplate(templatePath string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
return nil, errors.New("模板文件不存在")
|
return nil, NewFileNotFoundError(templatePath)
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
E "github.com/bestnite/sub2clash/error"
|
|
||||||
P "github.com/bestnite/sub2clash/model/proxy"
|
P "github.com/bestnite/sub2clash/model/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,13 +28,13 @@ func (p *AnytlsParser) GetType() string {
|
|||||||
|
|
||||||
func (p *AnytlsParser) Parse(proxy string) (P.Proxy, error) {
|
func (p *AnytlsParser) Parse(proxy string) (P.Proxy, error) {
|
||||||
if !hasPrefix(proxy, p.GetPrefixes()) {
|
if !hasPrefix(proxy, p.GetPrefixes()) {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidPrefix, Raw: proxy}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
link, err := url.Parse(proxy)
|
link, err := url.Parse(proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "url parse error",
|
Message: "url parse error",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -50,24 +49,24 @@ func (p *AnytlsParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
query := link.Query()
|
query := link.Query()
|
||||||
server := link.Hostname()
|
server := link.Hostname()
|
||||||
if server == "" {
|
if server == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server host",
|
Message: "missing server host",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
portStr := link.Port()
|
portStr := link.Port()
|
||||||
if portStr == "" {
|
if portStr == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server port",
|
Message: "missing server port",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
port, err := ParsePort(portStr)
|
port, err := ParsePort(portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidPort,
|
Type: ErrInvalidPort,
|
||||||
Raw: portStr,
|
Raw: portStr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package error
|
package parser
|
||||||
|
|
||||||
type ParseError struct {
|
type ParseError struct {
|
||||||
Type ParseErrorType
|
Type ParseErrorType
|
@ -6,7 +6,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
E "github.com/bestnite/sub2clash/error"
|
|
||||||
P "github.com/bestnite/sub2clash/model/proxy"
|
P "github.com/bestnite/sub2clash/model/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,21 +29,21 @@ func (p *HysteriaParser) GetType() string {
|
|||||||
|
|
||||||
func (p *HysteriaParser) Parse(proxy string) (P.Proxy, error) {
|
func (p *HysteriaParser) Parse(proxy string) (P.Proxy, error) {
|
||||||
if !hasPrefix(proxy, p.GetPrefixes()) {
|
if !hasPrefix(proxy, p.GetPrefixes()) {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidPrefix, Raw: proxy}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
link, err := url.Parse(proxy)
|
link, err := url.Parse(proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "url parse error",
|
Message: "url parse error",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
server := link.Hostname()
|
server := link.Hostname()
|
||||||
if server == "" {
|
if server == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server host",
|
Message: "missing server host",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -52,8 +51,8 @@ func (p *HysteriaParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
|
|
||||||
portStr := link.Port()
|
portStr := link.Port()
|
||||||
if portStr == "" {
|
if portStr == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server port",
|
Message: "missing server port",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -61,8 +60,8 @@ func (p *HysteriaParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
|
|
||||||
port, err := ParsePort(portStr)
|
port, err := ParsePort(portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidPort,
|
Type: ErrInvalidPort,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
E "github.com/bestnite/sub2clash/error"
|
|
||||||
P "github.com/bestnite/sub2clash/model/proxy"
|
P "github.com/bestnite/sub2clash/model/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,13 +28,13 @@ func (p *Hysteria2Parser) GetType() string {
|
|||||||
|
|
||||||
func (p *Hysteria2Parser) Parse(proxy string) (P.Proxy, error) {
|
func (p *Hysteria2Parser) Parse(proxy string) (P.Proxy, error) {
|
||||||
if !hasPrefix(proxy, p.GetPrefixes()) {
|
if !hasPrefix(proxy, p.GetPrefixes()) {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidPrefix, Raw: proxy}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
link, err := url.Parse(proxy)
|
link, err := url.Parse(proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "url parse error",
|
Message: "url parse error",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -50,24 +49,24 @@ func (p *Hysteria2Parser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
query := link.Query()
|
query := link.Query()
|
||||||
server := link.Hostname()
|
server := link.Hostname()
|
||||||
if server == "" {
|
if server == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server host",
|
Message: "missing server host",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
portStr := link.Port()
|
portStr := link.Port()
|
||||||
if portStr == "" {
|
if portStr == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server port",
|
Message: "missing server port",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
port, err := ParsePort(portStr)
|
port, err := ParsePort(portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidPort,
|
Type: ErrInvalidPort,
|
||||||
Raw: portStr,
|
Raw: portStr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
E "github.com/bestnite/sub2clash/error"
|
|
||||||
P "github.com/bestnite/sub2clash/model/proxy"
|
P "github.com/bestnite/sub2clash/model/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -65,7 +64,7 @@ func GetAllParsers() map[string]ProxyParser {
|
|||||||
func ParseProxyWithRegistry(proxy string) (P.Proxy, error) {
|
func ParseProxyWithRegistry(proxy string) (P.Proxy, error) {
|
||||||
proxy = strings.TrimSpace(proxy)
|
proxy = strings.TrimSpace(proxy)
|
||||||
if proxy == "" {
|
if proxy == "" {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidStruct, Raw: proxy, Message: "empty proxy string"}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: "empty proxy string"}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找匹配的解析器
|
// 查找匹配的解析器
|
||||||
@ -75,5 +74,5 @@ func ParseProxyWithRegistry(proxy string) (P.Proxy, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidPrefix, Raw: proxy, Message: "unsupported protocol"}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy, Message: "unsupported protocol"}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
E "github.com/bestnite/sub2clash/error"
|
|
||||||
P "github.com/bestnite/sub2clash/model/proxy"
|
P "github.com/bestnite/sub2clash/model/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,7 +32,7 @@ func (p *ShadowsocksParser) GetType() string {
|
|||||||
// Parse 解析Shadowsocks代理
|
// Parse 解析Shadowsocks代理
|
||||||
func (p *ShadowsocksParser) Parse(proxy string) (P.Proxy, error) {
|
func (p *ShadowsocksParser) Parse(proxy string) (P.Proxy, error) {
|
||||||
if !hasPrefix(proxy, p.GetPrefixes()) {
|
if !hasPrefix(proxy, p.GetPrefixes()) {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidPrefix, Raw: proxy}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(proxy, "@") {
|
if !strings.Contains(proxy, "@") {
|
||||||
@ -46,8 +45,8 @@ func (p *ShadowsocksParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
}
|
}
|
||||||
d, err := DecodeBase64(s[0])
|
d, err := DecodeBase64(s[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "url parse error",
|
Message: "url parse error",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -60,8 +59,8 @@ func (p *ShadowsocksParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
}
|
}
|
||||||
link, err := url.Parse(proxy)
|
link, err := url.Parse(proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "url parse error",
|
Message: "url parse error",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -69,8 +68,8 @@ func (p *ShadowsocksParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
|
|
||||||
server := link.Hostname()
|
server := link.Hostname()
|
||||||
if server == "" {
|
if server == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server host",
|
Message: "missing server host",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -78,16 +77,16 @@ func (p *ShadowsocksParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
|
|
||||||
portStr := link.Port()
|
portStr := link.Port()
|
||||||
if portStr == "" {
|
if portStr == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server port",
|
Message: "missing server port",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
port, err := ParsePort(portStr)
|
port, err := ParsePort(portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,8 +107,8 @@ func (p *ShadowsocksParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
if isLikelyBase64(password) {
|
if isLikelyBase64(password) {
|
||||||
password, err = DecodeBase64(password)
|
password, err = DecodeBase64(password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "password decode error",
|
Message: "password decode error",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
E "github.com/bestnite/sub2clash/error"
|
|
||||||
P "github.com/bestnite/sub2clash/model/proxy"
|
P "github.com/bestnite/sub2clash/model/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,7 +28,7 @@ func (p *ShadowsocksRParser) GetType() string {
|
|||||||
|
|
||||||
func (p *ShadowsocksRParser) Parse(proxy string) (P.Proxy, error) {
|
func (p *ShadowsocksRParser) Parse(proxy string) (P.Proxy, error) {
|
||||||
if !hasPrefix(proxy, p.GetPrefixes()) {
|
if !hasPrefix(proxy, p.GetPrefixes()) {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidPrefix, Raw: proxy}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, prefix := range p.GetPrefixes() {
|
for _, prefix := range p.GetPrefixes() {
|
||||||
@ -41,8 +40,8 @@ func (p *ShadowsocksRParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
|
|
||||||
proxy, err := DecodeBase64(proxy)
|
proxy, err := DecodeBase64(proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidBase64,
|
Type: ErrInvalidBase64,
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,16 +53,16 @@ func (p *ShadowsocksRParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
obfs := parts[4]
|
obfs := parts[4]
|
||||||
password, err := DecodeBase64(parts[5])
|
password, err := DecodeBase64(parts[5])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
port, err := ParsePort(parts[1])
|
port, err := ParsePort(parts[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidPort,
|
Type: ErrInvalidPort,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -75,8 +74,8 @@ func (p *ShadowsocksRParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
if len(serverInfoAndParams) == 2 {
|
if len(serverInfoAndParams) == 2 {
|
||||||
params, err := url.ParseQuery(serverInfoAndParams[1])
|
params, err := url.ParseQuery(serverInfoAndParams[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrCannotParseParams,
|
Type: ErrCannotParseParams,
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}
|
}
|
||||||
@ -93,8 +92,8 @@ func (p *ShadowsocksRParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
remarks = server + ":" + strconv.Itoa(port)
|
remarks = server + ":" + strconv.Itoa(port)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
E "github.com/bestnite/sub2clash/error"
|
|
||||||
P "github.com/bestnite/sub2clash/model/proxy"
|
P "github.com/bestnite/sub2clash/model/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,37 +27,37 @@ func (p *SocksParser) GetType() string {
|
|||||||
|
|
||||||
func (p *SocksParser) Parse(proxy string) (P.Proxy, error) {
|
func (p *SocksParser) Parse(proxy string) (P.Proxy, error) {
|
||||||
if !hasPrefix(proxy, p.GetPrefixes()) {
|
if !hasPrefix(proxy, p.GetPrefixes()) {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidPrefix, Raw: proxy}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
link, err := url.Parse(proxy)
|
link, err := url.Parse(proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "url parse error",
|
Message: "url parse error",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
server := link.Hostname()
|
server := link.Hostname()
|
||||||
if server == "" {
|
if server == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server host",
|
Message: "missing server host",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
portStr := link.Port()
|
portStr := link.Port()
|
||||||
if portStr == "" {
|
if portStr == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server port",
|
Message: "missing server port",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
port, err := ParsePort(portStr)
|
port, err := ParsePort(portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidPort,
|
Type: ErrInvalidPort,
|
||||||
Raw: portStr,
|
Raw: portStr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,8 +74,8 @@ func (p *SocksParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
decodeStr, err := DecodeBase64(encodeStr)
|
decodeStr, err := DecodeBase64(encodeStr)
|
||||||
splitStr := strings.Split(decodeStr, ":")
|
splitStr := strings.Split(decodeStr, ":")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "url parse error",
|
Message: "url parse error",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
E "github.com/bestnite/sub2clash/error"
|
|
||||||
P "github.com/bestnite/sub2clash/model/proxy"
|
P "github.com/bestnite/sub2clash/model/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,13 +28,13 @@ func (p *TrojanParser) GetType() string {
|
|||||||
|
|
||||||
func (p *TrojanParser) Parse(proxy string) (P.Proxy, error) {
|
func (p *TrojanParser) Parse(proxy string) (P.Proxy, error) {
|
||||||
if !hasPrefix(proxy, p.GetPrefixes()) {
|
if !hasPrefix(proxy, p.GetPrefixes()) {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidPrefix, Raw: proxy}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
link, err := url.Parse(proxy)
|
link, err := url.Parse(proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "url parse error",
|
Message: "url parse error",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -44,16 +43,16 @@ func (p *TrojanParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
password := link.User.Username()
|
password := link.User.Username()
|
||||||
server := link.Hostname()
|
server := link.Hostname()
|
||||||
if server == "" {
|
if server == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server host",
|
Message: "missing server host",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
portStr := link.Port()
|
portStr := link.Port()
|
||||||
if portStr == "" {
|
if portStr == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server port",
|
Message: "missing server port",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -61,8 +60,8 @@ func (p *TrojanParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
|
|
||||||
port, err := ParsePort(portStr)
|
port, err := ParsePort(portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidPort,
|
Type: ErrInvalidPort,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
E "github.com/bestnite/sub2clash/error"
|
|
||||||
P "github.com/bestnite/sub2clash/model/proxy"
|
P "github.com/bestnite/sub2clash/model/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,13 +28,13 @@ func (p *VlessParser) GetType() string {
|
|||||||
|
|
||||||
func (p *VlessParser) Parse(proxy string) (P.Proxy, error) {
|
func (p *VlessParser) Parse(proxy string) (P.Proxy, error) {
|
||||||
if !hasPrefix(proxy, p.GetPrefixes()) {
|
if !hasPrefix(proxy, p.GetPrefixes()) {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidPrefix, Raw: proxy}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
link, err := url.Parse(proxy)
|
link, err := url.Parse(proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "url parse error",
|
Message: "url parse error",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -43,8 +42,8 @@ func (p *VlessParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
|
|
||||||
server := link.Hostname()
|
server := link.Hostname()
|
||||||
if server == "" {
|
if server == "" {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidStruct,
|
Type: ErrInvalidStruct,
|
||||||
Message: "missing server host",
|
Message: "missing server host",
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -52,8 +51,8 @@ func (p *VlessParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
portStr := link.Port()
|
portStr := link.Port()
|
||||||
port, err := ParsePort(portStr)
|
port, err := ParsePort(portStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidPort,
|
Type: ErrInvalidPort,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -127,8 +126,8 @@ func (p *VlessParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
|
|
||||||
hosts, err := url.QueryUnescape(host)
|
hosts, err := url.QueryUnescape(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrCannotParseParams,
|
Type: ErrCannotParseParams,
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
E "github.com/bestnite/sub2clash/error"
|
|
||||||
P "github.com/bestnite/sub2clash/model/proxy"
|
P "github.com/bestnite/sub2clash/model/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -48,7 +47,7 @@ func (p *VmessParser) GetType() string {
|
|||||||
|
|
||||||
func (p *VmessParser) Parse(proxy string) (P.Proxy, error) {
|
func (p *VmessParser) Parse(proxy string) (P.Proxy, error) {
|
||||||
if !hasPrefix(proxy, p.GetPrefixes()) {
|
if !hasPrefix(proxy, p.GetPrefixes()) {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidPrefix, Raw: proxy}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, prefix := range p.GetPrefixes() {
|
for _, prefix := range p.GetPrefixes() {
|
||||||
@ -59,13 +58,13 @@ func (p *VmessParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
}
|
}
|
||||||
base64, err := DecodeBase64(proxy)
|
base64, err := DecodeBase64(proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidBase64, Raw: proxy, Message: err.Error()}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidBase64, Raw: proxy, Message: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
var vmess VmessJson
|
var vmess VmessJson
|
||||||
err = json.Unmarshal([]byte(base64), &vmess)
|
err = json.Unmarshal([]byte(base64), &vmess)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidStruct, Raw: proxy, Message: err.Error()}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
var port int
|
var port int
|
||||||
@ -73,8 +72,8 @@ func (p *VmessParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
case string:
|
case string:
|
||||||
port, err = ParsePort(vmess.Port.(string))
|
port, err = ParsePort(vmess.Port.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{
|
return P.Proxy{}, &ParseError{
|
||||||
Type: E.ErrInvalidPort,
|
Type: ErrInvalidPort,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
Raw: proxy,
|
Raw: proxy,
|
||||||
}
|
}
|
||||||
@ -88,7 +87,7 @@ func (p *VmessParser) Parse(proxy string) (P.Proxy, error) {
|
|||||||
case string:
|
case string:
|
||||||
aid, err = strconv.Atoi(vmess.Aid.(string))
|
aid, err = strconv.Atoi(vmess.Aid.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, &E.ParseError{Type: E.ErrInvalidStruct, Raw: proxy, Message: err.Error()}
|
return P.Proxy{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
|
||||||
}
|
}
|
||||||
case float64:
|
case float64:
|
||||||
aid = int(vmess.Aid.(float64))
|
aid = int(vmess.Aid.(float64))
|
||||||
|
Reference in New Issue
Block a user