Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support listen on multiple addresses #13

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (a *App) runCommand(cmd *cobra.Command, args []string) {
}
}

err := a.server.Start()
err := a.server.Start(cmd.Context())
if err != nil {
printError(err)
os.Exit(1)
Expand Down
9 changes: 7 additions & 2 deletions pkg/server/options/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package options
import (
"fmt"
"net"
"strings"

"github.com/alauda/registry-auth/pkg/server"
"github.com/alauda/registry-auth/pkg/server/config"
Expand Down Expand Up @@ -53,8 +54,12 @@ func (o *ServerOptions) ApplyFlags() []error {
var errs []error

o.BindAddress = viper.GetString(configServerBindAddress)
if o.BindAddress != "" && net.ParseIP(o.BindAddress) == nil {
errs = append(errs, fmt.Errorf("--%s must be IP", flagServerBindAddress))
if o.BindAddress != "" {
for _, ip := range strings.Split(o.BindAddress, ",") {
if net.ParseIP(ip) == nil {
errs = append(errs, fmt.Errorf("--%s %s must be IP", flagServerBindAddress, ip))
}
}
}

o.Port = viper.GetInt(configServerPort)
Expand Down
50 changes: 39 additions & 11 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/url"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -171,8 +173,19 @@ func (s *Server) ApplyToServer() error {
return nil
}

func (s *Server) Start() error {
addr := fmt.Sprintf("%s:%d", s.ServerConfig.BindAddress, s.ServerConfig.Port)
func (s *Server) Start(ctx context.Context) error {
var addrs []string
if s.ServerConfig.BindAddress == "" {
addrs = append(addrs, ":"+strconv.Itoa(s.ServerConfig.Port))
} else {
for _, addr := range strings.Split(s.ServerConfig.BindAddress, ",") {
if ip := net.ParseIP(addr); ip != nil && ip.To4() == nil {
addr = "[" + addr + "]"
}
addrs = append(addrs, addr+":"+strconv.Itoa(s.ServerConfig.Port))
}
}

handler := s.Container()

connState := func(conn net.Conn, state http.ConnState) {
Expand All @@ -194,7 +207,6 @@ func (s *Server) Start() error {
}

if s.ServerConfig.TLSCertFile != "" && s.ServerConfig.TLSKeyFile != "" {

cert, err := tls.LoadX509KeyPair(s.ServerConfig.TLSCertFile, s.ServerConfig.TLSKeyFile)
if err != nil {
return err
Expand All @@ -212,15 +224,31 @@ func (s *Server) Start() error {
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{cert},
}
for _, addr := range addrs {
go func(addr string) {
listener, err := tls.Listen("tcp", addr, &config)
if err != nil {
logger.Fatal(fmt.Sprintf("failed to listen %s, err: %v", addr, err), zap.String("func", "Start"))
return
}

listener, err := tls.Listen("tcp", addr, &config)
if err != nil {
return err
srv := http.Server{Handler: handler, ConnState: connState}
if err := srv.Serve(listener); err != nil {
logger.Fatal(fmt.Sprintf("failed to serve at %s, err: %v", addr, err), zap.String("func", "Start"))
}
}(addr)
}
} else {
for _, addr := range addrs {
go func(addr string) {
srv := http.Server{Addr: addr, Handler: handler, ConnState: connState}
if err := srv.ListenAndServe(); err != nil {
logger.Fatal(fmt.Sprintf("failed to listen and serve at %s, err: %v", addr, err), zap.String("func", "Start"))
}
}(addr)
}

srv := http.Server{Handler: handler, ConnState: connState}
return srv.Serve(listener)
}
srv := http.Server{Addr: addr, Handler: handler, ConnState: connState}
return srv.ListenAndServe()

<-ctx.Done()
return nil
}
Loading