Skip to content

Commit

Permalink
Update v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyun-li committed Dec 30, 2022
1 parent 15ac268 commit 989cd83
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 37 deletions.
20 changes: 5 additions & 15 deletions auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"io/ioutil"
"os"
"sync"
"time"

Expand All @@ -30,29 +29,20 @@ var autoCtrl AutoCtrl
func AutoInit() {
autoCtrl.cache = make(map[string]LocalAccessInfo, 100)
syncFromFile()
go syncTask()
go syncAutoTask()
}

func cacheFileTimestamp() time.Time {
info, err := os.Stat(CACHE_FILE)
if err != nil {
logs.Warning(err.Error())
return time.Time{}
}
return info.ModTime()
}

func syncTask() {
time1 := cacheFileTimestamp()
func syncAutoTask() {
time1 := engin.GetFileTimestamp(CACHE_FILE)
for {
time.Sleep(time.Second)
time2 := cacheFileTimestamp()
time2 := engin.GetFileTimestamp(CACHE_FILE)
if time2 != time1 {
autoCtrl.Lock()
syncFromFile()
autoCtrl.Unlock()
}
time1 = cacheFileTimestamp()
time1 = time2
}
}

Expand Down
42 changes: 41 additions & 1 deletion domain.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package main

import (
"encoding/json"
"io/ioutil"
"strings"
"sync"
"time"

"github.com/astaxie/beego/logs"
"github.com/easymesh/autoproxy/engin"
)

type DomainCtrl struct {
Expand All @@ -15,9 +19,45 @@ type DomainCtrl struct {

var forwardCtrl DomainCtrl

func DomainInit(domain []string) {
func DomainInit(file string) {
forwardCtrl.cache = make(map[string]string, 100)
forwardCtrl.domain = make([]string, 100)
domainFromFile(file)
go syncDomainTask(file)
}

func syncDomainTask(file string) {
time1 := engin.GetFileTimestamp(file)
for {
time.Sleep(time.Second)
time2 := engin.GetFileTimestamp(file)
if time2 != time1 {
domainFromFile(file)
}
time1 = time2
}
}

func domainFromFile(file string) {
forwardCtrl.Lock()
defer forwardCtrl.Unlock()

body, err := ioutil.ReadFile(file)
if err != nil {
logs.Warning(err.Error())
return
}
var domain []string
err = json.Unmarshal(body, &domain)
if err != nil {
logs.Warning(err.Error())
return
}

forwardCtrl.cache = make(map[string]string, 100)
forwardCtrl.domain = domain

logs.Info("sync %d from domain file success", len(domain))
}

func domainGet(address string) string {
Expand Down
10 changes: 10 additions & 0 deletions engin/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"net"
"net/url"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -96,3 +97,12 @@ func Connect(acc *HttpAccess, in net.Conn, out net.Conn) {

logs.Info("connect %s <-> %s close", in.RemoteAddr(), out.RemoteAddr())
}

func GetFileTimestamp(file string) time.Time {
info, err := os.Stat(file)
if err != nil {
logs.Warning(err.Error())
return time.Time{}
}
return info.ModTime()
}
21 changes: 1 addition & 20 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -71,19 +69,6 @@ func parseAddress(addr string) (string, string, error) {
return strings.ToLower(ul.Scheme), engin.Address(ul), nil
}

func parseDomain(domain string) ([]string, error) {
body, err := ioutil.ReadFile(domain)
if err != nil {
return nil, err
}
var output []string
err = json.Unmarshal(body, &output)
if err != nil {
return nil, err
}
return output, nil
}

func LocalAccessInit(scheme string, address string, auth *engin.AuthInfo) (engin.Access, error) {
var tlsEnable bool
if scheme == "https" {
Expand Down Expand Up @@ -182,11 +167,7 @@ func main() {
switch strings.ToLower(RunMode) {
case "domain":
{
domainList, err := parseDomain(DomainFile)
if err != nil {
panic(err)
}
DomainInit(domainList)
DomainInit(DomainFile)
acc.ForwardHandlerSet(func(address string, r *http.Request) engin.Forward {
if DomainCheck(address) {
logs.Info("%s auto forward to remote proxy", address)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ func init() {
}

func VersionGet() string {
return "v1.5.1"
return "v1.6.0"
}

0 comments on commit 989cd83

Please sign in to comment.