Skip to content

Commit

Permalink
Merge branch 'Alpha' into Meta
Browse files Browse the repository at this point in the history
  • Loading branch information
Larvan2 committed Jan 2, 2024
2 parents bda71db + 4ee267c commit dc2108c
Show file tree
Hide file tree
Showing 74 changed files with 1,460 additions and 823 deletions.
45 changes: 21 additions & 24 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ on:
- Alpha

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
REGISTRY: ghcr.io
REGISTRY: docker.io
jobs:
Build:
permissions: write-all
Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:
target: "linux-mips-softfloat linux-mips-hardfloat linux-mipsle-softfloat linux-mipsle-hardfloat",
id: "4",
}
- { type: "WithoutCGO", target: "linux-386 linux-riscv64", id: "5" }
- { type: "WithoutCGO", target: "linux-386 linux-riscv64 linux-loong64", id: "5" }
- {
type: "WithoutCGO",
target: "freebsd-386 freebsd-amd64 freebsd-arm64",
Expand All @@ -75,17 +75,17 @@ jobs:
- { type: "WithoutCGO-GO120", target: "windows-amd64-compatible windows-amd64 windows-386",id: "2" }
# Go 1.20 is the last release that will run on macOS 10.13 High Sierra or 10.14 Mojave. Go 1.21 will require macOS 10.15 Catalina or later.
- { type: "WithoutCGO-GO120", target: "darwin-amd64 darwin-arm64 android-arm64",id: "3" }
- { type: "WithCGO", target: "windows/*", id: "1" }
- { type: "WithCGO", target: "linux/386", id: "2" }
- { type: "WithCGO", target: "linux/amd64", id: "3" }
- { type: "WithCGO", target: "linux/arm64,linux/riscv64", id: "4" }
- { type: "WithCGO", target: "linux/arm,", id: "5" }
- { type: "WithCGO", target: "linux/arm-6,linux/arm-7", id: "6" }
- { type: "WithCGO", target: "linux/mips,linux/mipsle", id: "7" }
- { type: "WithCGO", target: "linux/mips64", id: "8" }
- { type: "WithCGO", target: "linux/mips64le", id: "9" }
- { type: "WithCGO", target: "darwin-10.16/*", id: "10" }
- { type: "WithCGO", target: "android", id: "11" }
# - { type: "WithCGO", target: "windows/*", id: "1" }
# - { type: "WithCGO", target: "linux/386", id: "2" }
# - { type: "WithCGO", target: "linux/amd64", id: "3" }
# - { type: "WithCGO", target: "linux/arm64,linux/riscv64", id: "4" }
# - { type: "WithCGO", target: "linux/arm,", id: "5" }
# - { type: "WithCGO", target: "linux/arm-6,linux/arm-7", id: "6" }
# - { type: "WithCGO", target: "linux/mips,linux/mipsle", id: "7" }
# - { type: "WithCGO", target: "linux/mips64", id: "8" }
# - { type: "WithCGO", target: "linux/mips64le", id: "9" }
# - { type: "WithCGO", target: "darwin-10.16/*", id: "10" }
# - { type: "WithCGO", target: "android", id: "11" }

steps:
- name: Check out code into the Go module directory
Expand Down Expand Up @@ -118,17 +118,14 @@ jobs:
- name: Set ENV
run: |
sudo timedatectl set-timezone "Asia/Shanghai"
echo "NAME=mihomo" >> $GITHUB_ENV
echo "REPO=${{ github.repository }}" >> $GITHUB_ENV
echo "ShortSHA=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV
echo "BUILDTIME=$(date)" >> $GITHUB_ENV
echo "BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV
shell: bash

- name: Set ENV
run: |
echo "TAGS=with_gvisor,with_lwip" >> $GITHUB_ENV
echo "LDFLAGS=-X 'github.com/metacubex/mihomo/constant.Version=${VERSION}' -X 'github.com/metacubex/mihomo/constant.BuildTime=${BUILDTIME}' -w -s -buildid=" >> $GITHUB_ENV
echo "GOTOOLCHAIN=local" >> $GITHUB_ENV
shell: bash

- name: Setup Go
Expand Down Expand Up @@ -233,7 +230,7 @@ jobs:

Upload-Prerelease:
permissions: write-all
if: ${{ github.ref_type=='branch' && github.event_name != 'pull_request' }}
if: ${{ github.ref_type == 'branch' && !startsWith(github.event_name, 'pull_request') }}
needs: [Build]
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -309,7 +306,7 @@ jobs:
generate_release_notes: true

Docker:
if: ${{ github.event_name != 'pull_request' }}
if: ${{ !startsWith(github.event_name, 'pull_request') }}
permissions: write-all
needs: [Build]
runs-on: ubuntu-latest
Expand Down Expand Up @@ -349,12 +346,12 @@ jobs:
ls .
ls bin/
- name: login to ghcr.io
- name: login to docker REGISTRY
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_HUB_USER }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
Expand Down
130 changes: 46 additions & 84 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ const (
defaultHistoriesNum = 10
)

type extraProxyState struct {
history *queue.Queue[C.DelayHistory]
type internalProxyState struct {
alive atomic.Bool
history *queue.Queue[C.DelayHistory]
}

type Proxy struct {
C.ProxyAdapter
history *queue.Queue[C.DelayHistory]
alive atomic.Bool
url string
extra *xsync.MapOf[string, *extraProxyState]
history *queue.Queue[C.DelayHistory]
extra *xsync.MapOf[string, *internalProxyState]
}

// AliveForTestUrl implements C.Proxy
Expand Down Expand Up @@ -81,7 +80,6 @@ func (p *Proxy) DelayHistory() []C.DelayHistory {
for _, item := range queueM {
histories = append(histories, item)
}

return histories
}

Expand All @@ -92,73 +90,53 @@ func (p *Proxy) DelayHistoryForTestUrl(url string) []C.DelayHistory {
if state, ok := p.extra.Load(url); ok {
queueM = state.history.Copy()
}

if queueM == nil {
queueM = p.history.Copy()
}

histories := []C.DelayHistory{}
for _, item := range queueM {
histories = append(histories, item)
}
return histories
}

func (p *Proxy) ExtraDelayHistory() map[string][]C.DelayHistory {
extraHistory := map[string][]C.DelayHistory{}

p.extra.Range(func(k string, v *extraProxyState) bool {
// ExtraDelayHistories return all delay histories for each test URL
// implements C.Proxy
func (p *Proxy) ExtraDelayHistories() map[string]C.ProxyState {
histories := map[string]C.ProxyState{}

p.extra.Range(func(k string, v *internalProxyState) bool {
testUrl := k
state := v

histories := []C.DelayHistory{}
queueM := state.history.Copy()
var history []C.DelayHistory

for _, item := range queueM {
histories = append(histories, item)
history = append(history, item)
}

extraHistory[testUrl] = histories

histories[testUrl] = C.ProxyState{
Alive: state.alive.Load(),
History: history,
}
return true
})
return extraHistory
return histories
}

// LastDelay return last history record. if proxy is not alive, return the max value of uint16.
// LastDelayForTestUrl return last history record of the specified URL. if proxy is not alive, return the max value of uint16.
// implements C.Proxy
func (p *Proxy) LastDelay() (delay uint16) {
var max uint16 = 0xffff
if !p.alive.Load() {
return max
}

history := p.history.Last()
if history.Delay == 0 {
return max
}
return history.Delay
}

// LastDelayForTestUrl implements C.Proxy
func (p *Proxy) LastDelayForTestUrl(url string) (delay uint16) {
var max uint16 = 0xffff
var maxDelay uint16 = 0xffff

alive := p.alive.Load()
history := p.history.Last()
alive := false
var history C.DelayHistory

if state, ok := p.extra.Load(url); ok {
alive = state.alive.Load()
history = state.history.Last()
}

if !alive {
return max
}

if history.Delay == 0 {
return max
if !alive || history.Delay == 0 {
return maxDelay
}
return history.Delay
}
Expand All @@ -173,8 +151,8 @@ func (p *Proxy) MarshalJSON() ([]byte, error) {
mapping := map[string]any{}
_ = json.Unmarshal(inner, &mapping)
mapping["history"] = p.DelayHistory()
mapping["extra"] = p.ExtraDelayHistory()
mapping["alive"] = p.AliveForTestUrl(p.url)
mapping["extra"] = p.ExtraDelayHistories()
mapping["alive"] = p.alive.Load()
mapping["name"] = p.Name()
mapping["udp"] = p.SupportUDP()
mapping["xudp"] = p.SupportXUDP()
Expand All @@ -187,47 +165,32 @@ func (p *Proxy) MarshalJSON() ([]byte, error) {
func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (t uint16, err error) {
defer func() {
alive := err == nil
record := C.DelayHistory{Time: time.Now()}
if alive {
record.Delay = t
}

if len(p.url) == 0 || url == p.url {
p.alive.Store(alive)
record := C.DelayHistory{Time: time.Now()}
if alive {
record.Delay = t
}
p.history.Put(record)
if p.history.Len() > defaultHistoriesNum {
p.history.Pop()
}

// test URL configured by the proxy provider
if len(p.url) == 0 {
p.url = url
}
} else {
record := C.DelayHistory{Time: time.Now()}
if alive {
record.Delay = t
}
p.history.Put(record)
if p.history.Len() > defaultHistoriesNum {
p.history.Pop()
}
p.alive.Store(alive)
p.history.Put(record)
if p.history.Len() > defaultHistoriesNum {
p.history.Pop()
}

state, ok := p.extra.Load(url)
if !ok {
state = &extraProxyState{
history: queue.New[C.DelayHistory](defaultHistoriesNum),
alive: atomic.NewBool(true),
}
p.extra.Store(url, state)
state, ok := p.extra.Load(url)
if !ok {
state = &internalProxyState{
history: queue.New[C.DelayHistory](defaultHistoriesNum),
alive: atomic.NewBool(true),
}
p.extra.Store(url, state)
}

state.alive.Store(alive)
state.history.Put(record)
if state.history.Len() > defaultHistoriesNum {
state.history.Pop()
}
state.alive.Store(alive)
state.history.Put(record)
if state.history.Len() > defaultHistoriesNum {
state.history.Pop()
}

}()

unifiedDelay := UnifiedDelay.Load()
Expand Down Expand Up @@ -304,8 +267,7 @@ func NewProxy(adapter C.ProxyAdapter) *Proxy {
ProxyAdapter: adapter,
history: queue.New[C.DelayHistory](defaultHistoriesNum),
alive: atomic.NewBool(true),
url: "",
extra: xsync.NewMapOf[string, *extraProxyState]()}
extra: xsync.NewMapOf[string, *internalProxyState]()}
}

func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
Expand Down
57 changes: 57 additions & 0 deletions adapter/inbound/ipfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package inbound

import (
"net"
"net/netip"

C "github.com/metacubex/mihomo/constant"
)

var lanAllowedIPs []netip.Prefix
var lanDisAllowedIPs []netip.Prefix

func SetAllowedIPs(prefixes []netip.Prefix) {
lanAllowedIPs = prefixes
}

func SetDisAllowedIPs(prefixes []netip.Prefix) {
lanDisAllowedIPs = prefixes
}

func AllowedIPs() []netip.Prefix {
return lanAllowedIPs
}

func DisAllowedIPs() []netip.Prefix {
return lanDisAllowedIPs
}

func IsRemoteAddrDisAllowed(addr net.Addr) bool {
m := C.Metadata{}
if err := m.SetRemoteAddr(addr); err != nil {
return false
}
return isAllowed(m.AddrPort().Addr().Unmap()) && !isDisAllowed(m.AddrPort().Addr().Unmap())
}

func isAllowed(addr netip.Addr) bool {
if addr.IsValid() {
for _, prefix := range lanAllowedIPs {
if prefix.Contains(addr) {
return true
}
}
}
return false
}

func isDisAllowed(addr netip.Addr) bool {
if addr.IsValid() {
for _, prefix := range lanDisAllowedIPs {
if prefix.Contains(addr) {
return true
}
}
}
return false
}
Loading

0 comments on commit dc2108c

Please sign in to comment.