Skip to content

Commit 4f5cabc

Browse files
authored
feat: add h2c for http server (#8294)
* feat: add h2c for http server * chore(config): add EnableH2c option
1 parent a2f2662 commit 4f5cabc

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

cmd/server.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
ftpserver "github.com/KirCute/ftpserverlib-pasvportmap"
8-
"github.com/KirCute/sftpd-alist"
9-
"github.com/alist-org/alist/v3/internal/fs"
107
"net"
118
"net/http"
129
"os"
@@ -16,14 +13,19 @@ import (
1613
"syscall"
1714
"time"
1815

16+
ftpserver "github.com/KirCute/ftpserverlib-pasvportmap"
17+
"github.com/KirCute/sftpd-alist"
1918
"github.com/alist-org/alist/v3/cmd/flags"
2019
"github.com/alist-org/alist/v3/internal/bootstrap"
2120
"github.com/alist-org/alist/v3/internal/conf"
21+
"github.com/alist-org/alist/v3/internal/fs"
2222
"github.com/alist-org/alist/v3/pkg/utils"
2323
"github.com/alist-org/alist/v3/server"
2424
"github.com/gin-gonic/gin"
2525
log "github.com/sirupsen/logrus"
2626
"github.com/spf13/cobra"
27+
"golang.org/x/net/http2"
28+
"golang.org/x/net/http2/h2c"
2729
)
2830

2931
// ServerCmd represents the server command
@@ -47,11 +49,15 @@ the address is defined in config file`,
4749
r := gin.New()
4850
r.Use(gin.LoggerWithWriter(log.StandardLogger().Out), gin.RecoveryWithWriter(log.StandardLogger().Out))
4951
server.Init(r)
52+
var httpHandler http.Handler = r
53+
if conf.Conf.Scheme.EnableH2c {
54+
httpHandler = h2c.NewHandler(r, &http2.Server{})
55+
}
5056
var httpSrv, httpsSrv, unixSrv *http.Server
5157
if conf.Conf.Scheme.HttpPort != -1 {
5258
httpBase := fmt.Sprintf("%s:%d", conf.Conf.Scheme.Address, conf.Conf.Scheme.HttpPort)
5359
utils.Log.Infof("start HTTP server @ %s", httpBase)
54-
httpSrv = &http.Server{Addr: httpBase, Handler: r}
60+
httpSrv = &http.Server{Addr: httpBase, Handler: httpHandler}
5561
go func() {
5662
err := httpSrv.ListenAndServe()
5763
if err != nil && !errors.Is(err, http.ErrServerClosed) {
@@ -72,7 +78,7 @@ the address is defined in config file`,
7278
}
7379
if conf.Conf.Scheme.UnixFile != "" {
7480
utils.Log.Infof("start unix server @ %s", conf.Conf.Scheme.UnixFile)
75-
unixSrv = &http.Server{Handler: r}
81+
unixSrv = &http.Server{Handler: httpHandler}
7682
go func() {
7783
listener, err := net.Listen("unix", conf.Conf.Scheme.UnixFile)
7884
if err != nil {

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ require (
6868
golang.org/x/crypto v0.36.0
6969
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e
7070
golang.org/x/image v0.19.0
71-
golang.org/x/net v0.37.0
71+
golang.org/x/net v0.38.0
7272
golang.org/x/oauth2 v0.22.0
7373
golang.org/x/time v0.8.0
7474
google.golang.org/appengine v1.6.8

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
741741
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
742742
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
743743
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
744+
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
745+
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
744746
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
745747
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
746748
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

internal/conf/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Scheme struct {
3535
KeyFile string `json:"key_file" env:"KEY_FILE"`
3636
UnixFile string `json:"unix_file" env:"UNIX_FILE"`
3737
UnixFilePerm string `json:"unix_file_perm" env:"UNIX_FILE_PERM"`
38+
EnableH2c bool `json:"enable_h2c" env:"ENABLE_H2C"`
3839
}
3940

4041
type LogConfig struct {

0 commit comments

Comments
 (0)