package utils

import (
	"net/url"
	"strings"
)

type ProxyHandle struct {
}

func (ProxyHandle) ParseProxy(proxyStr string) (protocol, username, password, ip, port string, err error) {
	// 清理字符串中的多余字符（如末尾的 \n）
	proxyStr = strings.TrimSpace(proxyStr)

	// 解析 URL
	proxyURL, err := url.Parse(proxyStr)
	if err != nil {
		return "", "", "", "", "", err
	}

	// 提取协议（http/https）
	protocol = proxyURL.Scheme

	// 提取 IP 和端口
	host := proxyURL.Host
	if strings.Contains(host, ":") {
		parts := strings.Split(host, ":")
		ip = parts[0]
		port = parts[1]
	} else {
		ip = host
		port = "8080" // 默认端口
	}

	// 提取用户名和密码
	if proxyURL.User != nil {
		username = proxyURL.User.Username()
		password, _ = proxyURL.User.Password()
	}

	return protocol, username, password, ip, port, nil
}
