mirror of
https://github.com/Rayzggz/server_torii.git
synced 2025-06-22 15:01:31 +08:00
Compare commits
7 Commits
feature/do
...
dev
Author | SHA1 | Date | |
---|---|---|---|
1888f10b64 | |||
d622430a6f | |||
2db26b78a0 | |||
6c340966a1 | |||
d47938ba22 | |||
bd4bbb01c1 | |||
87fb76f157 |
49
.github/workflows/release-tag-version.yml
vendored
49
.github/workflows/release-tag-version.yml
vendored
@ -1,49 +0,0 @@
|
||||
name: release-tag-version
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
packages: write # to publish to ghcr.io
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- run: git fetch --unshallow --quiet --tags --force
|
||||
- uses: docker/setup-qemu-action@v3
|
||||
- uses: docker/setup-buildx-action@v3
|
||||
- uses: docker/metadata-action@v5
|
||||
id: meta
|
||||
with:
|
||||
images: |-
|
||||
ghcr.io/rayzggz/server_torii
|
||||
# this will generate tags in the following format:
|
||||
# latest
|
||||
# 1
|
||||
# 1.2
|
||||
# 1.2.3
|
||||
tags: |
|
||||
type=semver,pattern={{version}}
|
||||
type=semver,pattern={{major}}
|
||||
type=semver,pattern={{major}}.{{minor}}
|
||||
- name: Login to GHCR using PAT
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: build rootful docker image
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
platforms: linux/amd64,linux/arm64
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
32
Dockerfile
32
Dockerfile
@ -1,32 +0,0 @@
|
||||
ARG ALPINE_VERSION=3.21
|
||||
ARG GO_VERSION=1.23.5
|
||||
ARG AUTHOR=Rayzggz
|
||||
ARG SERVER_NAME=server_torii
|
||||
|
||||
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS builder
|
||||
|
||||
ARG ALPINE_VERSION
|
||||
ARG GO_VERSION
|
||||
ARG SERVER_NAME
|
||||
ARG TARGETOS
|
||||
ARG TARGETARCH
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN set -eux; \
|
||||
TARGETOS=${TARGETOS:-linux}; \
|
||||
TARGETARCH=${TARGETARCH:-amd64}; \
|
||||
echo "Building for TARGETOS=${TARGETOS} TARGETARCH=${TARGETARCH}"; \
|
||||
go mod tidy; \
|
||||
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="-s -w" -o /app/${SERVER_NAME}
|
||||
|
||||
FROM alpine:${ALPINE_VERSION} AS final
|
||||
|
||||
ARG SERVER_NAME
|
||||
|
||||
COPY --from=builder /app/${SERVER_NAME} /app/${SERVER_NAME}
|
||||
|
||||
EXPOSE 25555
|
||||
ENTRYPOINT ["/app/server_torii"]
|
@ -1,4 +0,0 @@
|
||||
secret_key: "0378b0f84c4310279918d71a5647ba5d"
|
||||
captcha_validate_time: 600
|
||||
captcha_challenge_session_timeout: 120
|
||||
hcaptcha_secret: ""
|
@ -1,4 +0,0 @@
|
||||
HTTPFloodSpeedLimit:
|
||||
- "150/10s"
|
||||
HTTPFloodSameURILimit:
|
||||
- "50/10s"
|
17
config_example/rules/Server.yml
Normal file
17
config_example/rules/Server.yml
Normal file
@ -0,0 +1,17 @@
|
||||
CAPTCHA:
|
||||
secret_key: "0378b0f84c4310279918d71a5647ba5d"
|
||||
captcha_validate_time: 600
|
||||
captcha_challenge_session_timeout: 120
|
||||
hcaptcha_secret: ""
|
||||
HTTPFlood:
|
||||
HTTPFloodSpeedLimit:
|
||||
- "150/10s"
|
||||
HTTPFloodSameURILimit:
|
||||
- "50/10s"
|
||||
VerifyBot:
|
||||
verify_google_bot: true
|
||||
verify_bing_bot: true
|
||||
verify_baidu_bot: true
|
||||
verify_yandex_bot: true
|
||||
verify_sogou_bot: true
|
||||
verify_apple_bot: true
|
@ -1,6 +0,0 @@
|
||||
verify_google_bot: true
|
||||
verify_bing_bot: true
|
||||
verify_baidu_bot: true
|
||||
verify_yandex_bot: true
|
||||
verify_sogou_bot: true
|
||||
verify_apple_bot: true
|
@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v3"
|
||||
"net"
|
||||
"os"
|
||||
@ -27,6 +28,20 @@ type MainConfig struct {
|
||||
|
||||
// LoadMainConfig Read the configuration file and return the configuration object
|
||||
func LoadMainConfig(basePath string) (*MainConfig, error) {
|
||||
|
||||
defaultCfg := MainConfig{
|
||||
Port: "25555",
|
||||
WebPath: "/torii",
|
||||
RulePath: "/www/server_torii/config/rules",
|
||||
ErrorPage: "/www/server_torii/config/error_page",
|
||||
LogPath: "/www/server_torii/log/",
|
||||
NodeName: "Server Torii",
|
||||
ConnectingHostHeaders: []string{"Torii-Real-Host"},
|
||||
ConnectingIPHeaders: []string{"Torii-Real-IP"},
|
||||
ConnectingURIHeaders: []string{"Torii-Original-URI"},
|
||||
ConnectingCaptchaStatusHeaders: []string{"Torii-Captcha-Status"},
|
||||
}
|
||||
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -38,12 +53,12 @@ func LoadMainConfig(basePath string) (*MainConfig, error) {
|
||||
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return &defaultCfg, err
|
||||
}
|
||||
|
||||
var cfg MainConfig
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, err
|
||||
return &defaultCfg, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
@ -60,6 +75,18 @@ type RuleSet struct {
|
||||
HTTPFloodRule *dataType.HTTPFloodRule
|
||||
}
|
||||
|
||||
// ruleSetWrapper
|
||||
type ruleSetWrapper struct {
|
||||
CAPTCHARule *dataType.CaptchaRule `yaml:"CAPTCHA"`
|
||||
VerifyBotRule *dataType.VerifyBotRule `yaml:"VerifyBot"`
|
||||
HTTPFloodRule httpFloodRuleWrapper `yaml:"HTTPFlood"`
|
||||
}
|
||||
|
||||
type httpFloodRuleWrapper struct {
|
||||
HTTPFloodSpeedLimit []string `yaml:"HTTPFloodSpeedLimit"`
|
||||
HTTPFloodSameURILimit []string `yaml:"HTTPFloodSameURILimit"`
|
||||
}
|
||||
|
||||
// LoadRules Load all rules from the specified path
|
||||
func LoadRules(rulePath string) (*RuleSet, error) {
|
||||
rs := RuleSet{
|
||||
@ -73,73 +100,75 @@ func LoadRules(rulePath string) (*RuleSet, error) {
|
||||
}
|
||||
|
||||
// Load IP Allow List
|
||||
ipAllowFile := rulePath + "/IP_AllowList.conf"
|
||||
ipAllowFile := filepath.Join(rulePath, "/IP_AllowList.conf")
|
||||
if err := loadIPRules(ipAllowFile, rs.IPAllowTrie); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Load IP Block List
|
||||
ipBlockFile := rulePath + "/IP_BlockList.conf"
|
||||
ipBlockFile := filepath.Join(rulePath, "/IP_BlockList.conf")
|
||||
if err := loadIPRules(ipBlockFile, rs.IPBlockTrie); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Load URL Allow List
|
||||
urlAllowFile := rulePath + "/URL_AllowList.conf"
|
||||
urlAllowFile := filepath.Join(rulePath, "/URL_AllowList.conf")
|
||||
if err := loadURLRules(urlAllowFile, rs.URLAllowList); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Load URL Block List
|
||||
urlBlockFile := rulePath + "/URL_BlockList.conf"
|
||||
urlBlockFile := filepath.Join(rulePath, "/URL_BlockList.conf")
|
||||
if err := loadURLRules(urlBlockFile, rs.URLBlockList); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Load CAPTCHA Rule
|
||||
captchaFile := rulePath + "/CAPTCHA.yml"
|
||||
if err := loadCAPTCHARule(captchaFile, rs.CAPTCHARule); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Load Verify Bot Rule
|
||||
verifyBotFile := rulePath + "/VerifyBot.yml"
|
||||
if err := loadVerifyBotRule(verifyBotFile, rs.VerifyBotRule); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Load HTTP Flood Rule
|
||||
httpFloodFile := rulePath + "/HTTPFlood.yml"
|
||||
if err := loadHTTPFloodRule(httpFloodFile, rs.HTTPFloodRule); err != nil {
|
||||
return nil, err
|
||||
YAMLFile := filepath.Join(rulePath, "Server.yml")
|
||||
set, err := loadServerRules(YAMLFile, rs)
|
||||
if err != nil {
|
||||
return set, err
|
||||
}
|
||||
|
||||
return &rs, nil
|
||||
}
|
||||
|
||||
func loadCAPTCHARule(file string, rule *dataType.CaptchaRule) error {
|
||||
data, err := os.ReadFile(file)
|
||||
func loadServerRules(YAMLFile string, rs RuleSet) (*RuleSet, error) {
|
||||
yamlData, err := os.ReadFile(YAMLFile)
|
||||
if err != nil {
|
||||
return err
|
||||
if os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("[ERROR] rules file %s does not exist: %w", YAMLFile, err)
|
||||
} else {
|
||||
return nil, fmt.Errorf("[ERROR] failed to read rules file %s: %w", YAMLFile, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(data, &rule); err != nil {
|
||||
return err
|
||||
var wrapper ruleSetWrapper
|
||||
if err := yaml.Unmarshal(yamlData, &wrapper); err != nil {
|
||||
return nil, fmt.Errorf("[ERROR] failed to parse rules file %s: %w", YAMLFile, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
*rs.CAPTCHARule = *wrapper.CAPTCHARule
|
||||
*rs.VerifyBotRule = *wrapper.VerifyBotRule
|
||||
|
||||
}
|
||||
rs.HTTPFloodRule.HTTPFloodSpeedLimit = make(map[int64]int64)
|
||||
rs.HTTPFloodRule.HTTPFloodSameURILimit = make(map[int64]int64)
|
||||
|
||||
func loadVerifyBotRule(file string, rule *dataType.VerifyBotRule) error {
|
||||
data, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
for _, s := range wrapper.HTTPFloodRule.HTTPFloodSpeedLimit {
|
||||
limit, seconds, err := utils.ParseRate(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rs.HTTPFloodRule.HTTPFloodSpeedLimit[seconds] = limit
|
||||
}
|
||||
if err := yaml.Unmarshal(data, &rule); err != nil {
|
||||
return err
|
||||
|
||||
for _, s := range wrapper.HTTPFloodRule.HTTPFloodSameURILimit {
|
||||
limit, seconds, err := utils.ParseRate(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rs.HTTPFloodRule.HTTPFloodSameURILimit[seconds] = limit
|
||||
}
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// loadIPRules read the IP rule file and insert the rules into the trie
|
||||
@ -217,43 +246,3 @@ func loadURLRules(filePath string, list *dataType.URLRuleList) error {
|
||||
|
||||
return scanner.Err()
|
||||
}
|
||||
|
||||
func loadHTTPFloodRule(file string, rule *dataType.HTTPFloodRule) error {
|
||||
data, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
type httpFloodRuleYAML struct {
|
||||
HTTPFloodSpeedLimit []string `yaml:"HTTPFloodSpeedLimit"`
|
||||
HTTPFloodSameURILimit []string `yaml:"HTTPFloodSameURILimit"`
|
||||
}
|
||||
|
||||
var ymlRule httpFloodRuleYAML
|
||||
err = yaml.Unmarshal(data, &ymlRule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rule.HTTPFloodSpeedLimit = make(map[int64]int64)
|
||||
rule.HTTPFloodSameURILimit = make(map[int64]int64)
|
||||
|
||||
for _, s := range ymlRule.HTTPFloodSpeedLimit {
|
||||
limit, seconds, err := utils.ParseRate(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rule.HTTPFloodSpeedLimit[seconds] = limit
|
||||
}
|
||||
|
||||
for _, s := range ymlRule.HTTPFloodSameURILimit {
|
||||
limit, seconds, err := utils.ParseRate(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rule.HTTPFloodSameURILimit[seconds] = limit
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user