Changeset 70 in code for trunk


Ignore:
Timestamp:
Feb 12, 2024, 2:42:11 AM (16 months ago)
Author:
yakumo.izuru
Message:

PixivFEからいくつか輸入しました

リクエストリミッターを構成可能にする
マニュアルページを更新する
エラーページを改善する
GoFiber v2.52.0 へのアップデート

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

Location:
trunk
Files:
2 added
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/INSTANCES.md

    r67 r70  
    11# List of known instances
    22
    3 * [tr.chaotic.ninja](https://tr.chaotic.ninja)
    4     * Location: Germany
    5     * Cloudflare: No
    6 
     3| Name                                        | Cloudflare? | Country | URL                      | Engines supported |
     4|---------------------------------------------|-------------|---------|--------------------------|-------------------|
     5| Chaotic Ninja Communication Network Limited | No          | Germany | https://tr.chaotic.ninja | Google, Reverso   |
     6|                                             |             |         |                          |                   |
  • trunk/README.md

    r67 r70  
    88### Other projects
    99* [Mozhi](https://codeberg.org/aryak/mozhi), also a fork of SimplyTranslate
     10
     11### Credits
     12* SimpleWeb (fattalion, ManeraKai, metalune)
     13* [VnPower](https://vnpower.exozy.me) ([PixivFE](https://codeberg.org/VnPower/PixivFE) developer), for most of the stuff I borrowed from his project, as we both use [Fiber](https://gofiber.io)
     14* [z3bra](http://z3bra.org), for the `.ini` loading function, and privilege dropping from [Partage](http://z3bra.org/partage/)
  • trunk/cmd/mai/main.go

    r68 r70  
    33import (
    44        "bytes"
    5         "fmt"
     5        "errors"
     6        "log"
    67        "net/http"
    78        "net/url"
     
    910        "time"
    1011        "runtime"
     12        "strings"
    1113        "syscall"
    1214
     
    1820        "github.com/gofiber/fiber/v2/middleware/logger"
    1921        "github.com/gofiber/fiber/v2/middleware/limiter"
     22        "github.com/gofiber/fiber/v2/middleware/recover"
    2023        "github.com/gofiber/template/html/v2"
    2124)
     
    2629)
    2730var conf struct {
     31        danmaku int
    2832        listen string
    2933        staticpath string
    3034        tmplpath string
    3135}
     36func MaiSkipLimiter(c *fiber.Ctx) bool {
     37        // Paths listed here are not considered for rate limiting
     38        path := c.Path()
     39        return strings.HasPrefix(path, "/static") ||
     40                strings.HasPrefix(path, "/docs")
     41}
    3242func main() {
    3343        parseFlags()
     
    3848
    3949        // Default settings
     50        conf.danmaku = 10
    4051        conf.listen = "127.0.0.1:5000"
    4152        conf.staticpath = "./static"
     
    4556                uid, gid, err := usergroupids(username, groupname)
    4657                if err != nil {
    47                         fmt.Println(err)
    48                         os.Exit(1)
     58                        log.Fatal(err)
    4959                }
    5060                syscall.Setuid(uid)
     
    5565        engine.AddFunc("inc", func(i int) int { return i + 1 })
    5666
    57         app := fiber.New(
     67        server := fiber.New(
    5868                fiber.Config{
    5969                        AppName: "Mai",
     
    6272                        ServerHeader: "Mai (using Fiber v2.x)",
    6373                        Views: engine,
    64         })
    65 
    66         app.Use(favicon.New(
     74                        ErrorHandler: func(c *fiber.Ctx, err error) error {
     75                                code := fiber.StatusInternalServerError
     76                                err = c.Status(code).Render("pages/error", fiber.Map{"Title": "Error", "Error": err})
     77                                if err != nil {
     78                                        return c.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
     79                                }
     80                                return nil
     81                        },
     82                })
     83
     84        server.Use(recover.New())
     85
     86        server.Use(favicon.New(
    6787                favicon.Config{
    6888                        File: conf.staticpath + "/favicon.ico",
     
    7090        ))
    7191
    72         app.Use(logger.New(
     92        server.Use(logger.New(
    7393                logger.Config{
    7494                        Format: "==> ${ip}:${port} ${status} - ${method} ${path}\n",
    7595                        DisableColors: true,
    7696                        Output: os.Stdout,
     97                        Next: MaiSkipLimiter,
    7798                },
    7899        ))
    79100
    80         app.Use(limiter.New(limiter.Config{
    81                 Max: 10,
    82                 Expiration: 180 * time.Second,
     101        server.Use(limiter.New(limiter.Config{
     102                Next: MaiSkipLimiter,
     103                Max: conf.danmaku,
     104                Expiration: 30 * time.Second,
     105                LimiterMiddleware: limiter.SlidingWindow{},
    83106                LimitReached: func(c *fiber.Ctx) error {
    84                         return c.SendStatus(429)
    85                         return c.SendFile(conf.tmplpath + "/429.html")
     107                        log.Println("Limit reached!")
     108                        return errors.New("You're firing way too many danmaku really fast!")
    86109                },
    87110        }))
    88111
    89         app.All("/", func(c *fiber.Ctx) error {
     112        server.All("/", func(c *fiber.Ctx) error {
    90113                engine := c.Cookies("engine")
    91114                if c.Query("engine") != "" {
     
    186209        })
    187210
    188         app.All("/api/translate", func(c *fiber.Ctx) error {
     211        server.All("/api/translate", func(c *fiber.Ctx) error {
    189212                from := ""
    190213                to := ""
     
    217240        })
    218241
    219         app.Get("/api/source_languages", func(c *fiber.Ctx) error {
     242        server.Get("/api/source_languages", func(c *fiber.Ctx) error {
    220243                engine := c.Query("engine")
    221244                if _, ok := engines.Engines[engine]; !ok || engine == "" {
     
    229252        })
    230253
    231         app.Get("/api/target_languages", func(c *fiber.Ctx) error {
     254        server.Get("/api/target_languages", func(c *fiber.Ctx) error {
    232255                engine := c.Query("engine")
    233256                if _, ok := engines.Engines[engine]; !ok || engine == "" {
     
    241264        })
    242265
    243         app.Get("/api/tts", func(c *fiber.Ctx) error {
     266        server.Get("/api/tts", func(c *fiber.Ctx) error {
    244267                engine := c.Query("engine")
    245268                if _, ok := engines.Engines[engine]; !ok || engine == "" {
     
    268291                }
    269292        })
    270         app.Get("/robots.txt", func(c *fiber.Ctx) error {
     293
     294        server.Get("/robots.txt", func(c *fiber.Ctx) error {
    271295                return c.SendString("User-Agent: *\nDisallow: /\n")
    272296        })
    273         app.Get("/version", func(c *fiber.Ctx) error {
     297
     298        server.Get("/toomanyrequests", func(c *fiber.Ctx) error {
     299                return c.SendFile(conf.tmplpath + "/429.html")
     300                return c.SendStatus(429)
     301        })
     302
     303        server.Get("/version", func(c *fiber.Ctx) error {
    274304                return c.JSON(fiber.Map{
    275305                        "fiberversion": fiber.Version,
     
    278308                })
    279309        })
    280         app.Post("/switchlanguages", func(c *fiber.Ctx) error {
     310
     311        server.Post("/switchlanguages", func(c *fiber.Ctx) error {
    281312                if c.Cookies("from") != "" {
    282313                        fromCookie := new(fiber.Cookie)
     
    295326                return c.Redirect("/")
    296327        })
    297         app.Static("/static", conf.staticpath, fiber.Static{
     328        server.Static("/static", conf.staticpath, fiber.Static{
    298329                Compress: true,
    299330                ByteRange: true,
    300331                Browse: true,
    301332        })
    302         app.Static("/docs", "./docs", fiber.Static{})
    303         app.Listen(conf.listen)
     333
     334        server.Static("/docs", "./docs", fiber.Static{})
     335
     336        server.Listen(conf.listen)
    304337}
  • trunk/cmd/mai/readconf.go

    r67 r70  
    1111                return err                                             
    1212        }
     13        conf.danmaku, _ = cfg.Section("mai").Key("danmaku").Int()
    1314        conf.listen = cfg.Section("mai").Key("listen").String()
    1415        conf.staticpath = cfg.Section("mai").Key("static").String()
  • trunk/go.mod

    r61 r70  
    55require (
    66        github.com/PuerkitoBio/goquery v1.8.1
    7         github.com/gofiber/fiber/v2 v2.50.0
     7        github.com/gofiber/fiber/v2 v2.52.0
    88        github.com/gofiber/template/html/v2 v2.0.5
    99        gopkg.in/ini.v1 v1.67.0
     
    1515        github.com/gofiber/template v1.8.2 // indirect
    1616        github.com/gofiber/utils v1.1.0 // indirect
    17         github.com/google/uuid v1.3.1 // indirect
    18         github.com/klauspost/compress v1.16.7 // indirect
     17        github.com/google/uuid v1.5.0 // indirect
     18        github.com/klauspost/compress v1.17.0 // indirect
    1919        github.com/mattn/go-colorable v0.1.13 // indirect
    20         github.com/mattn/go-isatty v0.0.19 // indirect
     20        github.com/mattn/go-isatty v0.0.20 // indirect
    2121        github.com/mattn/go-runewidth v0.0.15 // indirect
    2222        github.com/philhofer/fwd v1.1.2 // indirect
     
    2525        github.com/tinylib/msgp v1.1.8 // indirect
    2626        github.com/valyala/bytebufferpool v1.0.0 // indirect
    27         github.com/valyala/fasthttp v1.50.0 // indirect
     27        github.com/valyala/fasthttp v1.51.0 // indirect
    2828        github.com/valyala/tcplisten v1.0.0 // indirect
    29         golang.org/x/net v0.8.0 // indirect
    30         golang.org/x/sys v0.13.0 // indirect
     29        golang.org/x/net v0.17.0 // indirect
     30        golang.org/x/sys v0.15.0 // indirect
    3131)
  • trunk/go.sum

    r61 r70  
    66github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
    77github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
    8 github.com/gofiber/fiber/v2 v2.50.0 h1:ia0JaB+uw3GpNSCR5nvC5dsaxXjRU5OEu36aytx+zGw=
    9 github.com/gofiber/fiber/v2 v2.50.0/go.mod h1:21eytvay9Is7S6z+OgPi7c7n4++tnClWmhpimVHMimw=
     8github.com/gofiber/fiber/v2 v2.52.0 h1:S+qXi7y+/Pgvqq4DrSmREGiFwtB7Bu6+QFLuIHYw/UE=
     9github.com/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
    1010github.com/gofiber/template v1.8.2 h1:PIv9s/7Uq6m+Fm2MDNd20pAFFKt5wWs7ZBd8iV9pWwk=
    1111github.com/gofiber/template v1.8.2/go.mod h1:bs/2n0pSNPOkRa5VJ8zTIvedcI/lEYxzV3+YPXdBvq8=
     
    1414github.com/gofiber/utils v1.1.0 h1:vdEBpn7AzIUJRhe+CiTOJdUcTg4Q9RK+pEa0KPbLdrM=
    1515github.com/gofiber/utils v1.1.0/go.mod h1:poZpsnhBykfnY1Mc0KeEa6mSHrS3dV0+oBWyeQmb2e0=
    16 github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
    17 github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
    18 github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
    19 github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
     16github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
     17github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
     18github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
     19github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
    2020github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
    2121github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
    2222github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
    23 github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
    24 github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
     23github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
     24github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
    2525github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
    2626github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
     
    3636github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
    3737github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
    38 github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M=
    39 github.com/valyala/fasthttp v1.50.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
     38github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=
     39github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
    4040github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
    4141github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
     
    5151golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
    5252golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
    53 golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
    54 golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
     53golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
     54golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
    5555golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
    5656golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
     
    6666golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
    6767golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
    68 golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
    69 golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
     68golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
     69golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
    7070golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
    7171golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
  • trunk/mai.ini.5

    r67 r70  
    88.Ss [mai] section
    99.Bl -tag -width 11n
    10 .It listen
     10.It danmaku (int)
     11Control the maximum amount of requests
     12before a ratelimit happens
     13.It listen (string)
    1114HTTP port for the server to listen.
    1215Default is "localhost:5000"
    13 .It static
     16.It static (string)
    1417Directory where the static resources are located.
    1518Default is "./static"
    16 .It templates
     19.It templates (string)
    1720Directory where the templates are located.
    1821Default is "./views"
Note: See TracChangeset for help on using the changeset viewer.