Skip to content

Commit

Permalink
libcore: rm nekoray & update v2ray usage
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Feb 11, 2023
1 parent f0143b4 commit 47b14e5
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@ abstract class V2RayInstance(
protected open suspend fun loadConfig() {
NekoJSInterface.Default.destroyAllJsi()
Libcore.setEnableLog(DataStore.enableLog, DataStore.logBufSize)
Libcore.setConfig(
config.tryDomains.joinToString(","),
false,
isExpertFlavor && DataStore.appTrafficStatistics
)
Libcore.setConfig(config.tryDomains.joinToString(","), false)
v2rayPoint.setConnectionPoolEnabled(isExpertFlavor && DataStore.appTrafficStatistics)
v2rayPoint.loadConfig(config.config)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ class ConfigurationFragment @JvmOverloads constructor(

fun urlTest() {
stopService()
Libcore.setConfig("", true, false)
Libcore.setConfig("", true)

val test = TestDialog()
val dialog = test.builder.show()
Expand Down Expand Up @@ -937,7 +937,7 @@ class ConfigurationFragment @JvmOverloads constructor(
NekoJSInterface.Default.destroyAllJsi()
mainJob.cancel()
testJobs.forEach { it.cancel() }
Libcore.setConfig("", false, false)
Libcore.setConfig("", false)
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions libcore/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ func InitCore(internalAssets string, externalAssets string, prefix string, useOf
isBgProcess := strings.HasSuffix(process, ":bg")

// Working dir
if !device.IsNekoray {
os.Chdir(filepath.Join(cachePath, "../no_backup"))
}
os.Chdir(filepath.Join(cachePath, "../no_backup"))

// Set up log
SetEnableLog(enableLog, maxKB)
Expand Down Expand Up @@ -60,7 +58,7 @@ func InitCore(internalAssets string, externalAssets string, prefix string, useOf
setupV2rayFileSystem(internalAssets, externalAssets)
setupResolvers()

if time.Now().Unix() >= GetExpireTime() && !device.IsNekoray {
if time.Now().Unix() >= GetExpireTime() {
outdated = "Your version is too old! Please update!! 版本太旧,请升级!"
} else if time.Now().Unix() < (GetBuildTime() - 86400) {
outdated = "Wrong system time! 系统时间错误!"
Expand Down
2 changes: 1 addition & 1 deletion libcore/core_commit.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2231e957a9703d776836a73feee85b535d0954d9
67e97b4c2ed5c1e364e4dd0f0e473a3a33ca8050
2 changes: 0 additions & 2 deletions libcore/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"go.uber.org/automaxprocs/maxprocs"
)

var IsNekoray = false

func AutoGoMaxProcs() {
maxprocs.Set(maxprocs.Logger(func(string, ...interface{}) {}))
}
Expand Down
2 changes: 1 addition & 1 deletion libcore/import.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package libcore

import (
_ "libcore/v2ray"
_ "libcore/v2ray_sip"

_ "github.com/v2fly/v2ray-core/v5/main/distro/all"
)
4 changes: 0 additions & 4 deletions libcore/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,6 @@ func (lp *logfile) Write(p []byte) (n int, err error) {
}
}

if device.IsNekoray {
os.Stdout.Write(p)
}

//TODO log by entry, show color
if lp.f != nil {
return lp.f.Write(p)
Expand Down
10 changes: 4 additions & 6 deletions libcore/nyan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import (
"strings"

"github.com/sirupsen/logrus"
"github.com/v2fly/v2ray-core/v5/nekoutils"
)

func SetConfig(tryDomainStr string, disableExtraCoreLog bool, nekoConnLog bool) {
func SetConfig(tryDomainStr string, filterCoreLog bool) {
// this is orphan

tryDomains = strings.Split(tryDomainStr, ",")
staticHosts = make(map[string][]net.IP)

nekoutils.Connection_V2Ray_Enabled = nekoConnLog
ResetAllConnections(false)

if disableExtraCoreLog {
if filterCoreLog {
v2rayLogHook = func(s string) string {
patterns := []string{
"Logger closing",
Expand Down
78 changes: 8 additions & 70 deletions libcore/v2ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package libcore

import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"sort"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -118,9 +116,7 @@ func (instance *V2RayInstance) Close() error {
if !instance.ForTest {
atomic.StorePointer(&v2rayDNSClient, unsafe.Pointer(nil))
}
nekoutils.ConnectionLog_V2Ray.ResetConnections(uintptr(unsafe.Pointer(instance.Core)))
nekoutils.ConnectionPool_V2Ray.ResetConnections(uintptr(unsafe.Pointer(instance.Core)))
nekoutils.ConnectionPool_System.ResetConnections(uintptr(unsafe.Pointer(instance.Core)))
nekoutils.ResetConnections(uintptr(unsafe.Pointer(instance.Core)))
return instance.Core.Close()
}
return nil
Expand All @@ -143,75 +139,17 @@ func (instance *V2RayInstance) DialContext(ctx context.Context, destination net.

// Neko connections

func (instance *V2RayInstance) SetConnectionPoolEnabled(enable bool) {
nekoutils.SetConnectionPoolV2RayEnabled(uintptr(unsafe.Pointer(instance.Core)), enable)
nekoutils.ResetAllConnections(false)
}

func ResetAllConnections(system bool) {
if system {
nekoutils.ConnectionPool_System.ResetConnections(0)
} else {
nekoutils.ConnectionPool_V2Ray.ResetConnections(0)
nekoutils.ConnectionLog_V2Ray.ResetConnections(0)
}
nekoutils.ResetAllConnections(system)
}

func ListV2rayConnections() string {
list2 := make([]interface{}, 0)

rangeMap := func(m *sync.Map) []interface{} {
vs := make(map[uint32]interface{}, 0)
ks := make([]uint32, 0)

m.Range(func(key interface{}, value interface{}) bool {
if k, ok := key.(uint32); ok {
vs[k] = value
ks = append(ks, k)
}
return true
})

sort.Slice(ks, func(i, j int) bool { return ks[i] > ks[j] })

ret := make([]interface{}, 0)
for _, id := range ks {
ret = append(ret, vs[id])
}
return ret
}

addToList := func(list interface{}) {
for i, c := range list.([]interface{}) {
if i >= 100 { // too much
return
}
if c2, ok := c.(*nekoutils.ManagedV2rayConn); ok {
if c2.Tag == "dns-out" || c2.Tag == "direct" {
continue
}
item := &struct {
ID uint32
Dest string
RDest string
Uid uint32
Start int64
End int64
Tag string
}{
c2.ID(),
c2.Dest,
c2.RouteDest,
c2.InboundUid,
c2.StartTime,
c2.EndTime,
c2.Tag,
}
list2 = append(list2, item)
}
}
}

addToList(rangeMap(&nekoutils.ConnectionPool_V2Ray.Map))
addToList(rangeMap(&nekoutils.ConnectionLog_V2Ray.Map))

b, _ := json.Marshal(&list2)
return string(b)
return nekoutils.ListConnections(0)
}

func CloseV2rayConnection(id uint32) {
Expand Down
19 changes: 7 additions & 12 deletions libcore/v2ray_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package libcore
import (
"context"
"fmt"
"libcore/device"
"libcore/doh"
"libcore/protect"
"net"
Expand Down Expand Up @@ -99,9 +98,7 @@ func setupResolvers() {
internet.UseAlternativeSystemDNSDialer(underlyingDialer)

// "localhost" localDns lookup -> Underlying
if !device.IsNekoray {
localdns.SetLookupFunc(underlyingResolver.LookupIP)
}
localdns.SetLookupFunc(underlyingResolver.LookupIP)

// doh package
doh.SetDialContext(underlyingDialer.DialContext)
Expand Down Expand Up @@ -143,12 +140,10 @@ func setupResolvers() {
})

// UDP ListenPacket
if !device.IsNekoray {
internet.RegisterListenerController(func(network, address string, fd uintptr) error {
if protect.FdProtector != nil {
protect.FdProtector.Protect(int32(fd))
}
return nil
})
}
internet.RegisterListenerController(func(network, address string, fd uintptr) error {
if protect.FdProtector != nil {
protect.FdProtector.Protect(int32(fd))
}
return nil
})
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 47b14e5

Please sign in to comment.