-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(api/traffic): merge now and traffic * feat(api): dns statistic * feat(api): dns statistic * chore: upgrade dashboard
- Loading branch information
Showing
6 changed files
with
133 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package routes | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/render" | ||
"github.com/gorilla/websocket" | ||
"github.com/igoogolx/itun2socks/internal/dns" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func dnsRouter() http.Handler { | ||
r := chi.NewRouter() | ||
r.Get("/statistic", getDnsStatistic) | ||
return r | ||
} | ||
|
||
type Dns struct { | ||
Success int32 `json:"success"` | ||
Fail int32 `json:"fail"` | ||
} | ||
|
||
func getDnsStatistic(w http.ResponseWriter, r *http.Request) { | ||
var wsConn *websocket.Conn | ||
if websocket.IsWebSocketUpgrade(r) { | ||
var err error | ||
wsConn, err = upgrader.Upgrade(w, r, nil) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
if wsConn == nil { | ||
w.Header().Set("Content-Type", "application/json") | ||
render.Status(r, http.StatusOK) | ||
} | ||
|
||
tick := time.NewTicker(time.Second) | ||
defer tick.Stop() | ||
buf := &bytes.Buffer{} | ||
var err error | ||
for range tick.C { | ||
buf.Reset() | ||
if err := json.NewEncoder(buf).Encode(dns.GetStatistic()); err != nil { | ||
break | ||
} | ||
|
||
if wsConn == nil { | ||
_, err = w.Write(buf.Bytes()) | ||
w.(http.Flusher).Flush() | ||
} else { | ||
err = wsConn.WriteMessage(websocket.TextMessage, buf.Bytes()) | ||
} | ||
|
||
if err != nil { | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package dns | ||
|
||
import ( | ||
"github.com/igoogolx/itun2socks/internal/constants" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
type StatisticItem struct { | ||
Success *atomic.Int32 `json:"success"` | ||
Fail *atomic.Int32 `json:"fail"` | ||
} | ||
|
||
type Statistic struct { | ||
Proxy StatisticItem `json:"proxy"` | ||
Direct StatisticItem `json:"direct"` | ||
} | ||
|
||
var static = Statistic{ | ||
Proxy: StatisticItem{ | ||
Success: atomic.NewInt32(0), | ||
Fail: atomic.NewInt32(0), | ||
}, | ||
Direct: StatisticItem{ | ||
Success: atomic.NewInt32(0), | ||
Fail: atomic.NewInt32(0), | ||
}, | ||
} | ||
|
||
func countSuccessQuery(policy constants.Policy) { | ||
if policy == constants.PolicyDirect { | ||
static.Direct.Success.Inc() | ||
} else if policy == constants.PolicyProxy { | ||
static.Proxy.Success.Inc() | ||
} | ||
} | ||
|
||
func countFailQuery(policy constants.Policy) { | ||
if policy == constants.PolicyDirect { | ||
static.Direct.Fail.Inc() | ||
} else if policy == constants.PolicyProxy { | ||
static.Proxy.Fail.Inc() | ||
} | ||
} | ||
|
||
func GetStatistic() Statistic { | ||
return static | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters