AndroidLibXrayLite/libv2ray_main.go

316 lines
7.6 KiB
Go
Raw Normal View History

2020-11-26 13:35:52 +08:00
package libv2ray
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
2025-01-07 13:45:25 +08:00
mobasset "golang.org/x/mobile/asset"
2020-11-26 13:35:52 +08:00
2020-12-04 11:10:47 +08:00
v2net "github.com/xtls/xray-core/common/net"
v2filesystem "github.com/xtls/xray-core/common/platform/filesystem"
"github.com/xtls/xray-core/common/serial"
2024-05-12 11:15:04 +08:00
v2core "github.com/xtls/xray-core/core"
2020-12-04 11:10:47 +08:00
v2stats "github.com/xtls/xray-core/features/stats"
v2serial "github.com/xtls/xray-core/infra/conf/serial"
_ "github.com/xtls/xray-core/main/distro/all"
v2internet "github.com/xtls/xray-core/transport/internet"
2020-11-26 13:35:52 +08:00
2020-12-04 11:10:47 +08:00
v2applog "github.com/xtls/xray-core/app/log"
v2commlog "github.com/xtls/xray-core/common/log"
2020-11-26 13:35:52 +08:00
)
const (
2024-05-12 11:15:04 +08:00
v2Asset = "xray.location.asset"
v2Cert = "xray.location.cert"
2023-12-03 14:28:36 -05:00
xudpBaseKey = "xray.xudp.basekey"
2020-11-26 13:35:52 +08:00
)
2025-03-28 05:23:10 +03:30
// V2RayPoint represents a V2Ray Point Server
2020-11-26 13:35:52 +08:00
type V2RayPoint struct {
SupportSet V2RayVPNServiceSupportsSet
statsManager v2stats.Manager
2022-06-26 20:18:07 +08:00
dialer *ProtectedDialer
2020-11-26 13:35:52 +08:00
v2rayOP sync.Mutex
closeChan chan struct{}
Vpoint *v2core.Instance
IsRunning bool
DomainName string
ConfigureFileContent string
AsyncResolve bool
}
2025-03-28 05:23:10 +03:30
// V2RayVPNServiceSupportsSet is an interface to support Android VPN mode
2020-11-26 13:35:52 +08:00
type V2RayVPNServiceSupportsSet interface {
Setup(Conf string) int
Prepare() int
Shutdown() int
Protect(int) bool
OnEmitStatus(int, string) int
}
2025-03-28 05:23:10 +03:30
// RunLoop runs the V2Ray main loop
2020-11-26 13:35:52 +08:00
func (v *V2RayPoint) RunLoop(prefIPv6 bool) (err error) {
v.v2rayOP.Lock()
defer v.v2rayOP.Unlock()
if v.IsRunning {
2025-03-28 05:23:10 +03:30
return nil
}
v.closeChan = make(chan struct{})
v.dialer.PrepareResolveChan()
go v.handleResolve()
prepareDomain := func() {
v.dialer.PrepareDomain(v.DomainName, v.closeChan, prefIPv6)
close(v.dialer.ResolveChan())
2020-11-26 13:35:52 +08:00
}
if v.AsyncResolve {
go prepareDomain()
} else {
prepareDomain()
}
err = v.pointloop()
2020-11-26 13:35:52 +08:00
return
}
2025-03-28 05:23:10 +03:30
// handleResolve handles the resolution process for domains
func (v *V2RayPoint) handleResolve() {
select {
case <-v.dialer.ResolveChan():
if !v.dialer.IsVServerReady() {
2025-03-28 05:23:10 +03:30
log.Println("vServer cannot resolve, shutting down")
v.StopLoop()
v.SupportSet.Shutdown()
}
case <-v.closeChan:
}
}
2025-03-28 05:23:10 +03:30
// StopLoop stops the V2Ray main loop
func (v *V2RayPoint) StopLoop() error {
2020-11-26 13:35:52 +08:00
v.v2rayOP.Lock()
defer v.v2rayOP.Unlock()
2025-03-28 05:23:10 +03:30
2020-11-26 13:35:52 +08:00
if v.IsRunning {
close(v.closeChan)
v.shutdownInit()
v.SupportSet.OnEmitStatus(0, "Closed")
}
2025-03-28 05:23:10 +03:30
return nil
2020-11-26 13:35:52 +08:00
}
2025-03-28 05:23:10 +03:30
// QueryStats returns the traffic stats for a given tag and direction
2020-11-26 13:35:52 +08:00
func (v V2RayPoint) QueryStats(tag string, direct string) int64 {
if v.statsManager == nil {
return 0
}
counter := v.statsManager.GetCounter(fmt.Sprintf("outbound>>>%s>>>traffic>>>%s", tag, direct))
if counter == nil {
return 0
}
return counter.Set(0)
}
2025-03-28 05:23:10 +03:30
// shutdownInit shuts down the V2Ray instance and cleans up resources
2020-11-26 13:35:52 +08:00
func (v *V2RayPoint) shutdownInit() {
2025-03-28 05:23:10 +03:30
if v.Vpoint != nil {
v.Vpoint.Close()
v.Vpoint = nil
}
2020-11-26 13:35:52 +08:00
v.IsRunning = false
v.statsManager = nil
}
2025-03-28 05:23:10 +03:30
// pointloop sets up and starts the V2Ray core
2020-11-26 13:35:52 +08:00
func (v *V2RayPoint) pointloop() error {
2025-03-28 05:23:10 +03:30
log.Println("Loading core config")
2020-11-26 13:35:52 +08:00
config, err := v2serial.LoadJSONConfig(strings.NewReader(v.ConfigureFileContent))
if err != nil {
2025-03-28 05:23:10 +03:30
return fmt.Errorf("failed to load core config: %w", err)
2020-11-26 13:35:52 +08:00
}
2025-03-28 05:23:10 +03:30
log.Println("Creating new core instance")
2020-11-26 13:35:52 +08:00
v.Vpoint, err = v2core.New(config)
if err != nil {
2025-03-28 05:23:10 +03:30
return fmt.Errorf("failed to create core instance: %w", err)
2020-11-26 13:35:52 +08:00
}
v.statsManager = v.Vpoint.GetFeature(v2stats.ManagerType()).(v2stats.Manager)
2025-03-28 05:23:10 +03:30
log.Println("Starting core")
2020-11-26 13:35:52 +08:00
v.IsRunning = true
if err := v.Vpoint.Start(); err != nil {
v.IsRunning = false
2025-03-28 05:23:10 +03:30
return fmt.Errorf("failed to start core: %w", err)
2020-11-26 13:35:52 +08:00
}
v.SupportSet.Prepare()
v.SupportSet.Setup("")
v.SupportSet.OnEmitStatus(0, "Running")
return nil
}
2025-03-28 05:23:10 +03:30
// MeasureDelay measures the delay to a given URL
2024-05-12 11:15:04 +08:00
func (v *V2RayPoint) MeasureDelay(url string) (int64, error) {
2020-11-26 13:35:52 +08:00
ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second)
2025-03-28 05:23:10 +03:30
defer cancel()
2020-11-26 13:35:52 +08:00
go func() {
select {
case <-v.closeChan:
cancel()
case <-ctx.Done():
}
}()
2024-05-12 11:15:04 +08:00
return measureInstDelay(ctx, v.Vpoint, url)
2020-11-26 13:35:52 +08:00
}
2025-03-28 05:23:10 +03:30
// InitV2Env sets the V2Ray asset path
2023-12-03 14:28:36 -05:00
func InitV2Env(envPath string, key string) {
2020-11-26 13:35:52 +08:00
if len(envPath) > 0 {
os.Setenv(v2Asset, envPath)
os.Setenv(v2Cert, envPath)
2020-11-26 13:35:52 +08:00
}
2023-12-03 14:28:36 -05:00
if len(key) > 0 {
os.Setenv(xudpBaseKey, key)
}
2020-11-26 13:35:52 +08:00
v2filesystem.NewFileReader = func(path string) (io.ReadCloser, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
_, file := filepath.Split(path)
return mobasset.Open(file)
}
return os.Open(path)
}
}
2025-03-28 05:23:10 +03:30
// MeasureOutboundDelay measures the outbound delay for a given configuration and URL
2024-05-12 11:15:04 +08:00
func MeasureOutboundDelay(ConfigureFileContent string, url string) (int64, error) {
2020-11-26 13:35:52 +08:00
config, err := v2serial.LoadJSONConfig(strings.NewReader(ConfigureFileContent))
if err != nil {
2025-03-28 05:23:10 +03:30
return -1, fmt.Errorf("failed to load JSON config: %w", err)
2020-11-26 13:35:52 +08:00
}
config.Inbound = nil
var essentialApp []*serial.TypedMessage
for _, app := range config.App {
if app.Type == "xray.app.proxyman.OutboundConfig" || app.Type == "xray.app.dispatcher.Config" || app.Type == "xray.app.log.Config" {
essentialApp = append(essentialApp, app)
}
}
config.App = essentialApp
2020-11-26 13:35:52 +08:00
inst, err := v2core.New(config)
if err != nil {
2025-03-28 05:23:10 +03:30
return -1, fmt.Errorf("failed to create core instance: %w", err)
2020-11-26 13:35:52 +08:00
}
inst.Start()
2025-03-28 05:23:10 +03:30
defer inst.Close()
return measureInstDelay(context.Background(), inst, url)
2020-11-26 13:35:52 +08:00
}
2025-03-28 05:23:10 +03:30
// NewV2RayPoint creates a new V2RayPoint instance
2020-11-26 13:35:52 +08:00
func NewV2RayPoint(s V2RayVPNServiceSupportsSet, adns bool) *V2RayPoint {
v2applog.RegisterHandlerCreator(v2applog.LogType_Console,
func(lt v2applog.LogType,
options v2applog.HandlerCreatorOptions) (v2commlog.Handler, error) {
return v2commlog.NewLogger(createStdoutLogWriter()), nil
})
dialer := NewProtectedDialer(s)
2020-11-26 13:35:52 +08:00
v2internet.UseAlternativeSystemDialer(dialer)
return &V2RayPoint{
SupportSet: s,
dialer: dialer,
AsyncResolve: adns,
}
}
2025-03-28 05:23:10 +03:30
// CheckVersionX returns the library and V2Ray versions
2020-11-26 13:35:52 +08:00
func CheckVersionX() string {
var version = 30
2022-06-26 20:18:07 +08:00
return fmt.Sprintf("Lib v%d, Xray-core v%s", version, v2core.Version())
2020-11-26 13:35:52 +08:00
}
2025-03-28 05:23:10 +03:30
// measureInstDelay measures the delay for an instance to a given URL
2024-05-12 11:15:04 +08:00
func measureInstDelay(ctx context.Context, inst *v2core.Instance, url string) (int64, error) {
2020-11-26 13:35:52 +08:00
if inst == nil {
2025-03-28 05:23:10 +03:30
return -1, errors.New("core instance is nil")
2020-11-26 13:35:52 +08:00
}
tr := &http.Transport{
TLSHandshakeTimeout: 6 * time.Second,
DisableKeepAlives: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
dest, err := v2net.ParseDestination(fmt.Sprintf("%s:%s", network, addr))
if err != nil {
return nil, err
}
return v2core.Dial(ctx, inst, dest)
},
}
2025-03-28 05:23:10 +03:30
client := &http.Client{
2020-11-26 13:35:52 +08:00
Transport: tr,
Timeout: 12 * time.Second,
}
2025-03-28 05:23:10 +03:30
if len(url) == 0 {
2024-05-12 11:15:04 +08:00
url = "https://www.google.com/generate_204"
}
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
2020-11-26 13:35:52 +08:00
start := time.Now()
2025-03-28 05:23:10 +03:30
resp, err := client.Do(req)
2020-11-26 13:35:52 +08:00
if err != nil {
return -1, err
}
2025-03-28 05:23:10 +03:30
defer resp.Body.Close()
2024-05-12 11:15:04 +08:00
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
2025-03-28 05:23:10 +03:30
return -1, fmt.Errorf("unexpected status code: %s", resp.Status)
2020-11-26 13:35:52 +08:00
}
return time.Since(start).Milliseconds(), nil
}
2022-06-26 20:18:07 +08:00
2025-03-28 05:23:10 +03:30
// consoleLogWriter creates our own log writer without datetime stamp
2022-06-26 20:18:07 +08:00
// As Android adds time stamps on each line
type consoleLogWriter struct {
logger *log.Logger
}
func (w *consoleLogWriter) Write(s string) error {
w.logger.Print(s)
return nil
}
func (w *consoleLogWriter) Close() error {
return nil
}
2025-03-28 05:23:10 +03:30
// createStdoutLogWriter creates a logger that won't print date/time stamps
2022-06-26 20:18:07 +08:00
func createStdoutLogWriter() v2commlog.WriterCreator {
return func() v2commlog.Writer {
return &consoleLogWriter{
2025-03-28 05:23:10 +03:30
logger: log.New(os.Stdout, "", 0),
}
2022-06-26 20:18:07 +08:00
}
2024-05-12 11:15:04 +08:00
}