Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions service/autoproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,20 @@ func addPerHost(p *proxy.PerHost, s string, custom bool) *proxy.PerHost {
if custom {
p.AddFromString(s)
} else {
for _, i := range txt.ReadAll(strings.NewReader(s)) {
if strings.HasSuffix(i, "@cn") {
continue
}
i = strings.ReplaceAll(i, ":@ads", "")
switch {
case strings.HasPrefix(i, "domain:"):
p.AddZone(strings.TrimPrefix(i, "domain:"))
case strings.HasPrefix(i, "full:"):
p.AddHost(strings.TrimPrefix(i, "full:"))
if res, err := txt.ReadAll(strings.NewReader(s)); err != nil {
errorLogger.Print(err)
} else {
for _, i := range res {
if strings.HasSuffix(i, "@cn") {
continue
}
i = strings.ReplaceAll(i, ":@ads", "")
switch {
case strings.HasPrefix(i, "domain:"):
p.AddZone(strings.TrimPrefix(i, "domain:"))
case strings.HasPrefix(i, "full:"):
p.AddHost(strings.TrimPrefix(i, "full:"))
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/fsnotify/fsnotify v1.9.0
github.com/sunshineplan/httpproxy v0.0.0-00010101000000-000000000000
github.com/sunshineplan/limiter v1.0.0
github.com/sunshineplan/service v1.0.22
github.com/sunshineplan/utils v0.1.80
github.com/sunshineplan/service v1.0.24
github.com/sunshineplan/utils v0.1.82
golang.org/x/net v0.46.0
golang.org/x/time v0.14.0
)
Expand Down
8 changes: 4 additions & 4 deletions service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/sunshineplan/limiter v1.0.0 h1:wx3q5eS5J+ggXlAxzg9k6UbDyJYrysNmHyxt5cNmCP8=
github.com/sunshineplan/limiter v1.0.0/go.mod h1:+Pjd5Pu7i5YclrnFz+MBFxGB9+MZ2cytQeV+S9kXOxY=
github.com/sunshineplan/service v1.0.22 h1:MFXKIbcd3POiJb9KRI8oeybc2qN0p79vv9f+OL88EDU=
github.com/sunshineplan/service v1.0.22/go.mod h1:JR7cTSNJVlajjXfXobA3Rr46z88M+6VK6coxq7JmyrY=
github.com/sunshineplan/utils v0.1.80 h1:dVGZqO3HvwxqyDwuRLwCm2QMZp7Wx2//F15PnD4wOt4=
github.com/sunshineplan/utils v0.1.80/go.mod h1:R7MInPRKnExzzNGJ9qY1W0+11P/3jc6IO5ICuGDPA1w=
github.com/sunshineplan/service v1.0.24 h1:Ma+75ZUUmMjucWg98aJIJFntLgo0TNsVPeWUuZYIOMo=
github.com/sunshineplan/service v1.0.24/go.mod h1:XJwWAFNd4gEYHl47KgG2zhm2Y8e5RN/En0vIpqd7C4U=
github.com/sunshineplan/utils v0.1.82 h1:GmRfNLxhsKfkroRHZHTdxNE9GcF77t5+A2rLOZnwtmM=
github.com/sunshineplan/utils v0.1.82/go.mod h1:R7MInPRKnExzzNGJ9qY1W0+11P/3jc6IO5ICuGDPA1w=
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=
golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210=
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
Expand Down
6 changes: 3 additions & 3 deletions service/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (limit limit) isExceeded(record *record) bool {
if limit.daily == 0 && limit.monthly == 0 {
return false
}
if limit.daily == 0 || record.today.Load() < int64(limit.daily) {
return record.monthly.Load() >= int64(limit.monthly)
if limit.daily == 0 || record.today.Get() < int64(limit.daily) {
return record.monthly.Get() >= int64(limit.monthly)
}
return record.today.Load() >= int64(limit.daily)
return record.today.Get() >= int64(limit.daily)
}
18 changes: 11 additions & 7 deletions service/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func checkDayChange() {
t := time.Now()
if now.YearDay() != t.YearDay() {
recordMap.Range(func(_ user, v *record) bool {
v.today.Add(-v.today.Load())
v.today.Add(-v.today.Get())
if t.Day() == 1 {
v.monthly.Add(-v.monthly.Load())
v.monthly.Add(-v.monthly.Get())
}
return true
})
Expand All @@ -43,7 +43,7 @@ type record struct {
}

func (r *record) writer(w io.Writer) io.Writer {
return r.today.AddWriter(r.monthly.AddWriter(r.total.AddWriter(w)))
return counter.CountWriter(counter.CountWriter(counter.CountWriter(w, &r.total), &r.monthly), &r.today)
}

func store(user user, today, monthly, total int64) *record {
Expand Down Expand Up @@ -90,7 +90,7 @@ func parseRecord(rows []string) {
continue
}
}
if t == now {
if t.Equal(now) {
today, err = strconv.ParseInt(s[1], 10, 64)
if err != nil {
errorLogger.Println(row, err)
Expand All @@ -116,13 +116,13 @@ func saveRecord(base *Base) {

base.accounts.Range(func(a auth.Basic, _ *limit) bool {
if v, ok := recordMap.Load(user{a.Username, false}); ok {
fmt.Fprintf(zw, "%s:%d:%d:%d\n", a.Username, v.today.Load(), v.monthly.Load(), v.total.Load())
fmt.Fprintf(zw, "%s:%d:%d:%d\n", a.Username, v.today.Get(), v.monthly.Get(), v.total.Get())
}
return true
})
base.whitelist.Range(func(a allow, _ *limit) bool {
if v, ok := recordMap.Load(user{string(a), true}); ok {
fmt.Fprintf(zw, "%s[w]:%d:%d:%d\n", a, v.today.Load(), v.monthly.Load(), v.total.Load())
fmt.Fprintf(zw, "%s[w]:%d:%d:%d\n", a, v.today.Get(), v.monthly.Get(), v.total.Get())
}
return true
})
Expand All @@ -138,7 +138,11 @@ func initRecord(base *Base) {
defer f.Close()
if zr, err := gzip.NewReader(f); err == nil {
defer zr.Close()
parseRecord(txt.ReadAll(zr))
if rows, err := txt.ReadAll(zr); err == nil {
parseRecord(rows)
} else {
errorLogger.Print(err)
}
} else {
errorLogger.Print(err)
}
Expand Down
10 changes: 5 additions & 5 deletions service/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func getUsage(user user) *usage {
if v, ok := recordMap.Load(user); ok {
res := usagePool.Get()
res.user = user
res.today = unit.ByteSize(v.today.Load())
res.monthly = unit.ByteSize(v.monthly.Load())
res.total = unit.ByteSize(v.total.Load())
res.today = unit.ByteSize(v.today.Get())
res.monthly = unit.ByteSize(v.monthly.Get())
res.total = unit.ByteSize(v.total.Get())
return res
}
return nil
Expand Down Expand Up @@ -119,8 +119,8 @@ func saveStatus(base *Base, servers []*httpsvr.Server) {
fmt.Fprintln(f, "Throughput:")
var send, receive int64
for _, i := range servers {
send += i.WriteCount()
receive += i.ReadCount()
send += i.WriteBytes()
receive += i.WriteBytes()
}
fmt.Fprintf(f, "Send: %s Receive: %s\n", unit.ByteSize(send), unit.ByteSize(receive))
fmt.Fprintln(f)
Expand Down