Skip to content

Commit

Permalink
fix: AIT-50130, support multi addresses (#13)
Browse files Browse the repository at this point in the history
Signed-off-by: tfwang <[email protected]>
  • Loading branch information
davidwtf authored Sep 4, 2024
1 parent 6601996 commit 4ce8e10
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
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
}

0 comments on commit 4ce8e10

Please sign in to comment.