-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
105 lines (89 loc) · 2.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2023-2024 The chatgpt_reverse_proxy Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"github.com/lenye/chatgpt_reverse_proxy/internal/config"
"github.com/lenye/chatgpt_reverse_proxy/internal/env"
"github.com/lenye/chatgpt_reverse_proxy/internal/proxy"
"github.com/lenye/chatgpt_reverse_proxy/pkg/version"
)
func main() {
var (
showVersion = flag.Bool("version", false, "print version string")
)
flag.Parse()
if *showVersion {
fmt.Print(version.Print())
return
}
start := time.Now()
if err := config.Read(); err != nil {
slog.Error("config.Read failed", "error", err)
os.Exit(1)
return
}
// Target
oxyTarget, err := url.Parse(config.Target)
if err != nil {
slog.Error(fmt.Sprintf("invalid %s=%s", env.Target, config.Target), "error", err)
os.Exit(1)
return
}
slog.Info("Configuration",
slog.Group("config", "target", config.Target, "port", config.WebPort, "hop_http_header_prefix", config.HopHttpHeaderPrefix))
var lc net.ListenConfig
// 主动启用mptcp
lc.SetMultipathTCP(true)
isMultipathTCP := lc.MultipathTCP()
slog.Debug("net.ListenConfig", "MultipathTCP", isMultipathTCP)
ln, err := lc.Listen(context.Background(), "tcp", ":"+config.WebPort)
if err != nil {
slog.Error("server listen failed", "error", err)
os.Exit(1)
return
}
slog.Info("server listening on " + ln.Addr().String())
oxy := proxy.BuildSingleHostProxy(oxyTarget)
srv := &http.Server{Addr: ":" + config.WebPort, Handler: oxy}
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, syscall.SIGINT, syscall.SIGTERM)
s := <-sigint
slog.Debug(fmt.Sprintf("received signal: %v", s))
// We received an interrupt signal, shut down.
if err := srv.Shutdown(context.Background()); err != nil {
// Error from closing listeners, or context timeout:
slog.Error("server shutdown failed", "error", err)
}
}()
if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("server failed", "error", err)
}
slog.Info("server stopped")
slog.Info(version.AppName+" exit",
"start", start, "uptime", time.Since(start),
)
}