goweber

package module
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2025 License: MIT Imports: 21 Imported by: 0

README

goweber

介绍

goweber是一個GO編寫的WEB框架,主要用於API服務。 支持功能:

  • 路由
  • 日志服務
  • 配置服务
  • 基於IP的限流
  • 中間件服務,支持全局和路由級
  • 支持JWT
  • 用戶行爲監控
數據結構

数据结构

安装教程
go get github.com/sonkwl/goweber
使用说明
package goweber

import (
    "fmt"
    "net/http"
    "github.com/sonkwl/goweber"
)
func main() { 
    app := goweber.New()
    app.Get("/", func(w http.ResponseWriter, r *http.Request) { 
        fmt.Fprintf(w, "Hello World!")
    })
    app.Run()
}

配置文件

請保證config.ini在執行文件同目錄下

[server]
# 網站端口
# Website port
port = 8080

#  網絡日誌 logfile:目錄,logmax:文件最大大小
# Network log logfile: directory, logmax: maximum file size
logfile = access.log
logmax = 1024000000

# 緩存器,單位Mb
# Cache, unit Mb
# apper中有Cache结构指针
# apper has Cache structure pointer
cache = 1

# 限流 ipmax監控最大IP數量>0,開啓限流,ratelimit每秒訪問次數
# Rate limiting ipmax: maximum number of monitored IPs > 0, enable rate limiting, ratelimit: visits per second
ipmax=1000
ratelimit=10

# apper中有Jwt结构指针
# apper has Jwt structure pointer
[jwt]
rint = 1
rstr = WHSS
version = V1
exp = 8

# apper中有File结构指针
# apper has File structure pointer
[file]
# 单位MB
# Unit MB
size = 20
# 最大批量上传数量
# Maximum batch upload quantity
max = 2
# 文件保存目录
# File save directory
path = ./files
# 文件类型
# File types
type = .jpg,.png

# 用戶行爲監控
# user behaver monitor
[behaver]
# 最大監控IP數量, 0表示不監控
# Maximum monitoring IP quantity, 0 means not monitored
ipmax = 1000
# 監控IP過期時間,秒
# Monitoring IP expiration time, seconds
expire = 300
# 監控IP清理間隔,秒
# Monitoring IP cleanup interval, seconds
cleansecond = 300

Documentation

Overview

*

安全策略,監控用戶的行爲。
危險行爲定義:
1.高頻請求
	- 限流處理,已實現,見apper.iplimiter
	- 掃描目錄,多次訪問不存在目錄,例如/project/user,/project/admin,404錯誤一個周期内超過50次,則封禁該ip
	- 遍歷爆破,例如:登錄接口多次頻繁嘗試密碼,采用時間間隔監控
2.輸入内容異常
	- 異常參數:sql注入,XSS,命令注入,// ! 待完善
	- 提交大文件/數據,// ! 待完善

方案:
IP監控周期5分鐘,禁用規則
1.404錯誤,超過50次
2.10次内的訪問時間間隔相同,或少於1秒的次數超過7次
3.url正則匹配,防止sql漏洞,XSS漏洞,命令 // ! 待完善

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Apper

type Apper struct {

	// 緩存器
	// Cache
	Cache *Cacher

	// jwt处理
	// JWT processing
	Jwt *Jwter
	// 文件上传
	// File upload
	File *FileUploader
	// 用戶行爲監控
	// user behavior monitoring
	Bh *Behaver
	// contains filtered or unexported fields
}

Apper 是应用程序的主结构体,包含路由映射、配置信息、端口、日志等信息 Apper is the main struct of the application, containing route mappings, configuration information, port, logs, etc.

func New

func New() *Apper

New 创建并初始化一个新的Apper实例 New creates and initializes a new Apper instance

func (*Apper) Close

func (this *Apper) Close()

Close 关闭应用程序资源,包括日志文件和消息通道 Close closes application resources, including log file and message channel

func (*Apper) Get

func (this *Apper) Get(path string, f http.HandlerFunc, mid ...MiddlewareFunc)

Get 注册GET请求的路由处理函数 Get registers the route handler function for GET requests

func (*Apper) GetClientIP

func (this *Apper) GetClientIP(r *http.Request) string

GetClientIP 获取客户端真实IP地址 GetClientIP get client real IP address

func (*Apper) Logger

func (this *Apper) Logger()

Logger 处理日志记录,监听消息通道并将日志写入文件或控制台 Logger handles log recording, listens to the message channel and writes logs to file or console

func (*Apper) Post

func (this *Apper) Post(path string, f http.HandlerFunc, mid ...MiddlewareFunc)

Post 注册POST请求的路由处理函数 Post registers the route handler function for POST requests

func (*Apper) Route

func (this *Apper) Route(method string, path string, f http.HandlerFunc, mid ...MiddlewareFunc)

Route 注册指定HTTP方法的路由处理函数 Route registers the route handler function for the specified HTTP method

func (*Apper) Run

func (this *Apper) Run()

Run 启动HTTP服务器 Run starts the HTTP server

func (*Apper) RunTLS

func (this *Apper) RunTLS(certFile, keyFile string)

RunTLS 启动HTTPS服务器 RunTLS starts the HTTPS server

func (*Apper) ServeHTTP

func (this *Apper) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP 实现http.Handler接口,处理HTTP请求 ServeHTTP implements the http.Handler interface to handle HTTP requests

func (*Apper) SetBehaver added in v0.1.6

func (this *Apper) SetBehaver()

SetBehaver

func (*Apper) SetCache

func (this *Apper) SetCache()

SetCache 缓存设置 SetCache cache settings

func (*Apper) SetConfig

func (this *Apper) SetConfig()

SetConfig 从config.ini文件中读取配置信息 SetConfig reads configuration information from config.ini file

func (*Apper) SetFile added in v0.1.4

func (this *Apper) SetFile()

SetFile 设置上传文件 SetFile set file upload

func (*Apper) SetJwt added in v0.1.4

func (this *Apper) SetJwt()

jwt 设置 JWT settings

func (*Apper) SetLimit

func (this *Apper) SetLimit()

SetLimit 設置限流 SetLimit set rate limiting

func (*Apper) SetLog

func (this *Apper) SetLog()

SetLog 根据配置设置日志记录器 SetLog sets up the logger according to configuration

func (*Apper) SetPort

func (this *Apper) SetPort()

SetPort 从配置中设置服务器端口,默认为8080 SetPort sets the server port from configuration, default is 8080

func (*Apper) Use

func (this *Apper) Use(middleware ...MiddlewareFunc)

全局中間件處理 Global middleware processing

type Behaver added in v0.1.6

type Behaver struct {
	IpMontion map[string]*IpInfo
	// 監控IP最大值
	IpMax int
	// 禁用IP
	IpDisable map[string]int64
	// 監控時間間隔
	Expire int64
	// 自動清理時間,分鐘
	CleanSecond int64
	// contains filtered or unexported fields
}

func NewBehaver added in v0.1.6

func NewBehaver() *Behaver

func (*Behaver) CheckNotFound added in v0.1.6

func (this *Behaver) CheckNotFound(ip string) bool

* 檢查404錯誤

func (*Behaver) CheckScan added in v0.1.6

func (this *Behaver) CheckScan(ip string) bool

* 10次内訪問時間間隔相同

func (*Behaver) Clear added in v0.1.6

func (this *Behaver) Clear()

* 防止Behaver數量溢出,每5分鐘清理

func (*Behaver) IsLock added in v0.1.6

func (this *Behaver) IsLock(ip string) bool

* IP是否鎖定

func (*Behaver) Lock added in v0.1.6

func (this *Behaver) Lock(ip string)

* 鎖定IP

func (*Behaver) Record added in v0.1.6

func (this *Behaver) Record(ip string, code int)

* 記錄該IP的行為

func (*Behaver) RegexUrl added in v0.1.6

func (this *Behaver) RegexUrl(url string) bool

* 正則匹配url ! 有問題,待完善

type Cacher

type Cacher struct {
	// contains filtered or unexported fields
}

func NewCacher

func NewCacher(m int64) *Cacher

* 新建緩存,m為空間大小,單位 m

func (*Cacher) IsCache

func (this *Cacher) IsCache(w http.ResponseWriter, r *http.Request) bool

* 判斷緩存是否存在,存在則返回緩存數據

func (*Cacher) SetCache

func (this *Cacher) SetCache(r *http.Request, m int64, data string)

* 根據url定義緩存時間和緩存數據,m為分鐘

func (*Cacher) Size

func (this *Cacher) Size() int64

* 獲取占用空間大小

type Configer

type Configer struct {
	// contains filtered or unexported fields
}

func (*Configer) Get

func (this *Configer) Get(title string, key string) string

func (*Configer) ReadFile

func (this *Configer) ReadFile() error

func (*Configer) SetFile

func (this *Configer) SetFile(f *os.File)

type FileUploader

type FileUploader struct {
	MaxSize      int64
	AllowedTypes []string
	SavePath     string
	MaxFiles     int    //一次最大上传文件数量
	FieldName    string // 添加单文件字段名
	FieldNames   string // 添加多文件字段名
	Keyword      string // 添加文件名关键字
}

func NewFileUploader

func NewFileUploader(maxSize int64, allowedTypes []string, savePath string) *FileUploader

func (*FileUploader) HandleUpload

func (f *FileUploader) HandleUpload(r *http.Request) ([]string, error)

HandleUpload 修改为支持批量文件上传

type IpInfo added in v0.1.6

type IpInfo struct {
	Time     int64
	NotFound int
	Times    []int64
}

type Jwter

type Jwter struct {
	Rint    int
	Rstr    string
	Exphour int
	Version string
	Key     string
}

func NewJwter

func NewJwter() *Jwter

func (*Jwter) Decode

func (this *Jwter) Decode(token string) (string, error)

func (*Jwter) Encode

func (this *Jwter) Encode() (string, error)

func (*Jwter) GetArr

func (this *Jwter) GetArr(tokenstr string) ([]string, error)

func (*Jwter) GetJoin

func (this *Jwter) GetJoin(key string, exp string, code string) (string, error)

func (*Jwter) OneToTwo

func (this *Jwter) OneToTwo(val string) string

func (*Jwter) Validate

func (this *Jwter) Validate(token string) error

type MiddlewareFunc

type MiddlewareFunc func(r *http.Request) error

中間件類型 Middleware type

type Token

type Token struct {
	Id   string
	Exp  string
	Code string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL