重命名包 client 为 edge;重命名包 server 为 gateway
This commit is contained in:
10
gateway/core/auth.go
Normal file
10
gateway/core/auth.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package core
|
||||
|
||||
import "time"
|
||||
|
||||
type Permit struct {
|
||||
Expire time.Time `json:"expire"`
|
||||
Whitelists []string `json:"whitelists"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
73
gateway/core/conn.go
Normal file
73
gateway/core/conn.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
Conn net.Conn
|
||||
Reader *bufio.Reader
|
||||
Tag [16]byte
|
||||
Protocol string
|
||||
Dest *FwdAddr
|
||||
Auth *AuthContext
|
||||
}
|
||||
|
||||
func (c Conn) Read(b []byte) (n int, err error) {
|
||||
return c.Reader.Read(b)
|
||||
}
|
||||
|
||||
func (c Conn) Write(b []byte) (n int, err error) {
|
||||
return c.Conn.Write(b)
|
||||
}
|
||||
|
||||
func (c Conn) Close() error {
|
||||
return c.Conn.Close()
|
||||
}
|
||||
|
||||
func (c Conn) LocalAddr() net.Addr {
|
||||
return c.Conn.LocalAddr()
|
||||
}
|
||||
|
||||
func (c Conn) RemoteAddr() net.Addr {
|
||||
return c.Conn.RemoteAddr()
|
||||
}
|
||||
|
||||
func (c Conn) SetDeadline(t time.Time) error {
|
||||
return c.Conn.SetDeadline(t)
|
||||
}
|
||||
|
||||
func (c Conn) SetReadDeadline(t time.Time) error {
|
||||
return c.Conn.SetReadDeadline(t)
|
||||
}
|
||||
|
||||
func (c Conn) SetWriteDeadline(t time.Time) error {
|
||||
return c.Conn.SetWriteDeadline(t)
|
||||
}
|
||||
|
||||
type FwdAddr struct {
|
||||
IP net.IP
|
||||
Port int
|
||||
Domain string
|
||||
}
|
||||
|
||||
func (a FwdAddr) Network() string {
|
||||
return "tcp"
|
||||
}
|
||||
|
||||
func (a FwdAddr) String() string {
|
||||
return fmt.Sprintf("%s:%d", a.IP, a.Port)
|
||||
}
|
||||
|
||||
type AuthContext struct {
|
||||
Timeout float64
|
||||
Payload Payload
|
||||
Meta map[string]any
|
||||
}
|
||||
|
||||
type Payload struct {
|
||||
ID int32
|
||||
}
|
||||
6
gateway/core/consts.go
Normal file
6
gateway/core/consts.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package core
|
||||
|
||||
const (
|
||||
Version = 1
|
||||
RestoreMagic = 0x72
|
||||
)
|
||||
65
gateway/core/map.go
Normal file
65
gateway/core/map.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package core
|
||||
|
||||
import "sync"
|
||||
|
||||
type SyncMap[K any, V any] struct {
|
||||
_map sync.Map
|
||||
}
|
||||
|
||||
func (m *SyncMap[K, V]) Store(key K, value V) {
|
||||
m._map.Store(key, value)
|
||||
}
|
||||
|
||||
func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) {
|
||||
v, ok := m._map.Load(key)
|
||||
if ok {
|
||||
value = v.(V)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *SyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool) {
|
||||
v, loaded := m._map.Swap(key, value)
|
||||
if loaded {
|
||||
previous = v.(V)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *SyncMap[K, V]) Delete(key K) {
|
||||
m._map.Delete(key)
|
||||
}
|
||||
|
||||
func (m *SyncMap[K, V]) Clear() {
|
||||
m._map.Clear()
|
||||
}
|
||||
|
||||
func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) {
|
||||
m._map.Range(func(k, v any) bool {
|
||||
return f(k.(K), v.(V))
|
||||
})
|
||||
}
|
||||
|
||||
func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
|
||||
v, loaded := m._map.LoadOrStore(key, value)
|
||||
if loaded {
|
||||
actual = v.(V)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, ok bool) {
|
||||
v, ok := m._map.LoadAndDelete(key)
|
||||
if ok {
|
||||
value = v.(V)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *SyncMap[K, V]) CompareAndSwap(key K, old, new V) (swapped bool) {
|
||||
return m._map.CompareAndSwap(key, old, new)
|
||||
}
|
||||
|
||||
func (m *SyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
|
||||
return m._map.CompareAndDelete(key, old)
|
||||
}
|
||||
74
gateway/core/security.go
Normal file
74
gateway/core/security.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
g "proxy-server/gateway/globals"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SecuredReq struct {
|
||||
Content string `json:"content"`
|
||||
Nonce string `json:"nonce"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
}
|
||||
|
||||
func Decrypt[T any](req *SecuredReq, secret string) (resp *T, err error) {
|
||||
|
||||
// 解密请求
|
||||
block, err := aes.NewCipher([]byte(secret))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var nonce = []byte(req.Nonce)
|
||||
|
||||
content, err := base64.StdEncoding.DecodeString(req.Content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var aad = []byte(fmt.Sprintf("%s:%d", req.Nonce, req.Timestamp))
|
||||
|
||||
bytes, err := gcm.Open(nil, nonce, content, aad)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 检查时间与 nonce 是否匹配
|
||||
var duration = time.Now().UnixMilli() - req.Timestamp
|
||||
if duration > 1000*60*5 { // 5分钟
|
||||
return nil, fmt.Errorf("请求超时,当前时间:%d,接收时间:%d", time.Now().UnixMilli(), req.Timestamp)
|
||||
}
|
||||
|
||||
result, err := g.Redis.Exists(context.Background(), req.Nonce).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result > 0 {
|
||||
return nil, fmt.Errorf("请求已被使用,nonce:%s", req.Nonce)
|
||||
}
|
||||
|
||||
// 将 nonce 存入 redis,设置过期时间为 5 分钟
|
||||
err = g.Redis.Set(context.Background(), req.Nonce, 1, time.Minute*5).Err()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 返回解密后的数据
|
||||
err = json.Unmarshal(bytes, &resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user