mirror of
https://github.com/v2rayA/v2rayA.git
synced 2026-11-20 23:02:25 +03:00
A start as an ordinary user without --lite died on Linux and Windows with a message telling the user to add --lite, and on macOS with a failure to write /etc/v2raya. Nothing that needs root works without it anyway, so the flag only chose the configuration directory and the dashboard's options; the binary can decide that from its own privileges. Lite mode is now implied when the process is not root or an administrator, with one warning in the log, and --lite keeps working. A service definition can then be a single file for both ways of running: brew services and a systemd user unit start the same command as root for the transparent proxy or as the user for the system proxy. The root checks in pre_environment.go became unreachable and are gone; --passcheckroot keeps its meaning. The v4 import consulted the v4 library on every fresh start. That library parses the command line itself and opens boltv4.db under its own default directory, /etc/v2raya, creating the file when the directory exists and dying when it does not, which an unprivileged start without --lite hit at once. It is now asked only when boltv4.db exists in the configuration directory, which also stops a fresh root install from growing an empty boltv4.db. Verified: macOS (arm64) and the Linux VM as an unprivileged user without --lite start in lite mode with the user's configuration directory and report lite: true, isRoot: false; the VM service as root is unchanged.
190 lines
6.6 KiB
Go
190 lines
6.6 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
jsonIteratorExtra "github.com/json-iterator/go/extra"
|
|
"github.com/v2rayA/v2rayA/conf"
|
|
"github.com/v2rayA/v2rayA/db"
|
|
"github.com/v2rayA/v2rayA/db/configure"
|
|
"github.com/v2rayA/v2rayA/kernel/v2ray"
|
|
"github.com/v2rayA/v2rayA/kernel/v2ray/asset"
|
|
"github.com/v2rayA/v2rayA/kernel/v2ray/asset/dat"
|
|
"github.com/v2rayA/v2rayA/kernel/v2ray/where"
|
|
"github.com/v2rayA/v2rayA/pkg/util/log"
|
|
"github.com/v2rayA/v2rayA/server/service"
|
|
|
|
touchv4 "github.com/v2rayA/v2rayA-lib4/core/touch"
|
|
configurev4 "github.com/v2rayA/v2rayA-lib4/db/configure"
|
|
servicev4 "github.com/v2rayA/v2rayA-lib4/server/service"
|
|
)
|
|
|
|
func initDBValue() {
|
|
log.Info("init DB")
|
|
err := configure.SetConfigure(configure.New())
|
|
if err != nil {
|
|
log.Fatal("initDBValue: %v", err)
|
|
}
|
|
}
|
|
|
|
func initConfigure() {
|
|
// initialize configuration
|
|
jsonIteratorExtra.RegisterFuzzyDecoders()
|
|
|
|
// Track whether we performed a BoltDB→SQLite migration in this session.
|
|
// If so, we must skip the v4 migration and initDBValue() below, because
|
|
// the data has already been migrated into SQLite by MigrateFromBoltDB().
|
|
migratedFromBoltDB := false
|
|
|
|
// Try to initialize SQLite database.
|
|
// If an old BoltDB database (bolt.db) exists, Open() returns ErrNeedMigration,
|
|
// and we must run the migration first.
|
|
err := db.Open()
|
|
if errors.Is(err, db.ErrNeedMigration) {
|
|
log.Warn("Detected legacy BoltDB database, migrating to SQLite...")
|
|
if err := db.MigrateFromBoltDB(); err != nil {
|
|
log.Fatal("Database migration failed: %v", err)
|
|
}
|
|
migratedFromBoltDB = true
|
|
// Migration succeeded (MigrateFromBoltDB already logged the completion);
|
|
// now initialize SQLite normally.
|
|
if err := db.Open(); err != nil {
|
|
// If SQLite initialization fails after migration, restore the backup
|
|
// so the migration can be retried on next startup.
|
|
confPath := conf.GetEnvironmentConfig().Config
|
|
backupPath := filepath.Join(confPath, "bolt.db.bak")
|
|
if _, e := os.Stat(backupPath); e == nil {
|
|
if renameErr := os.Rename(backupPath, filepath.Join(confPath, "bolt.db")); renameErr == nil {
|
|
log.Warn("Restored bolt.db from bolt.db.bak due to SQLite initialization failure")
|
|
}
|
|
}
|
|
log.Fatal("Failed to initialize SQLite after migration: %v", err)
|
|
}
|
|
} else if err != nil {
|
|
log.Fatal("Failed to initialize database: %v", err)
|
|
}
|
|
|
|
//db
|
|
dbPath := filepath.Join(conf.GetEnvironmentConfig().Config, "bolt.db")
|
|
if _, e := os.Lstat(dbPath); os.IsNotExist(e) {
|
|
// If we just migrated from BoltDB to SQLite, the data is already in
|
|
// SQLite. Do NOT run initDBValue() or v4 migration, as that would
|
|
// overwrite or clear the freshly migrated data.
|
|
if !migratedFromBoltDB {
|
|
// db.IsNewDB is true only when v2raya.db was just created by Open()
|
|
// (i.e., it did not exist before). In that case, we need to initialize
|
|
// the default configuration values.
|
|
// If db.IsNewDB is false, v2raya.db already existed (from a previous
|
|
// migration or a previous startup), so we skip initialization.
|
|
if db.IsNewDB {
|
|
// The v4 library parses the command line itself and opens
|
|
// boltv4.db under its own idea of the configuration directory,
|
|
// creating the file and dying when the directory is missing
|
|
// (/etc/v2raya as an unprivileged user, \etc\v2raya on
|
|
// Windows). Only ask it when a v4 database is actually here.
|
|
v4Path := filepath.Join(conf.GetEnvironmentConfig().Config, "boltv4.db")
|
|
if _, e := os.Stat(v4Path); e == nil && !configurev4.IsConfigureNotExists() {
|
|
// The v4 readers use boltv4.db directly. A bolt.db copy would
|
|
// make the next Open return ErrNeedMigration after SQLite exists.
|
|
log.Warn("Migrating from v4 to main")
|
|
// clear connects of outbounds
|
|
for _, out := range configure.GetOutbounds() {
|
|
_ = configure.ClearConnects(out)
|
|
}
|
|
var indexes []int
|
|
for i := 0; i < configurev4.GetLenServers(); i++ {
|
|
indexes = append(indexes, i)
|
|
}
|
|
_ = configure.RemoveServers(indexes)
|
|
|
|
indexes = nil
|
|
for i := 0; i < configurev4.GetLenSubscriptions(); i++ {
|
|
indexes = append(indexes, i)
|
|
}
|
|
_ = configure.RemoveSubscriptions(indexes)
|
|
|
|
// migrate servers and subscriptions
|
|
t := touchv4.GenerateTouch()
|
|
subs := configurev4.GetSubscriptionsV2()
|
|
for _, sub := range subs {
|
|
log.Info("Importing subscription: %v", sub.Address)
|
|
if e := service.ImportSubscription(sub.Address); e != nil {
|
|
log.Warn("Failed to migrate subscription: %v", sub.Address)
|
|
}
|
|
}
|
|
for iSvr := range t.Servers {
|
|
if addr, e := servicev4.GetSharingAddress(&configurev4.Which{
|
|
TYPE: configurev4.ServerType,
|
|
ID: iSvr + 1,
|
|
}); e == nil {
|
|
if e := service.ImportServer(addr, nil); e != nil {
|
|
log.Warn("Failed to migrate server: %v", addr)
|
|
}
|
|
}
|
|
}
|
|
|
|
log.Warn("Migration is done")
|
|
} else {
|
|
initDBValue()
|
|
}
|
|
}
|
|
}
|
|
|
|
// ensure the default "proxy" outbound group exists
|
|
if err := configure.InitDefaultOutbound(); err != nil {
|
|
log.Warn("initDefaultOutbound: %v", err)
|
|
}
|
|
}
|
|
|
|
if len(configure.GetTproxyWhiteIpGroups().CountryCodes) == 0 {
|
|
configure.SetTproxyWhiteIpGroups([]string{"PRIVATE"}, []string{})
|
|
}
|
|
|
|
// check if config.json exists
|
|
if _, err := os.Stat(asset.GetV2rayConfigPath()); err != nil {
|
|
// if not exists, create one. This mostly happens when mounting a volume in docker mode and it covers /etc/v2ray.
|
|
t := v2ray.Template{}
|
|
_ = v2ray.WriteV2rayConfig(t.ToConfigBytes())
|
|
}
|
|
|
|
// first determine if v2ray exists
|
|
if _, err := where.GetV2rayBinPath(); err == nil {
|
|
// check if geoip, geosite exist
|
|
if !asset.DoesV2rayAssetExist("geoip.dat") || !asset.DoesV2rayAssetExist("geosite.dat") {
|
|
log.Alert("downloading missing geoip.dat and geosite.dat")
|
|
var l net.Listener
|
|
if l, err = net.Listen("tcp", conf.GetEnvironmentConfig().Address); err != nil {
|
|
log.Fatal("net.Listen: %v", err)
|
|
}
|
|
e := gin.New()
|
|
e.GET("/", func(c *gin.Context) {
|
|
c.Header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
|
|
c.Header("Pragma", "no-cache")
|
|
c.Header("Expires", "0")
|
|
c.String(200, "Downloading missing geoip.dat and geosite.dat; refresh the page later.")
|
|
})
|
|
go e.RunListener(l)
|
|
if !asset.DoesV2rayAssetExist("geoip.dat") {
|
|
err := dat.UpdateLocalGeoIP()
|
|
if err != nil {
|
|
log.Fatal("UpdateLocalGeoIP: %v", err)
|
|
}
|
|
}
|
|
if !asset.DoesV2rayAssetExist("geosite.dat") {
|
|
err = dat.UpdateLocalGeoSite()
|
|
if err != nil {
|
|
log.Fatal("UpdateLocalGeoSite: %v", err)
|
|
}
|
|
}
|
|
if l != nil {
|
|
l.Close()
|
|
}
|
|
log.Alert("geoip.dat and geosite.dat are ready")
|
|
}
|
|
}
|
|
}
|