Skip to content

Commit

Permalink
Port domain proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
tecarter94 committed Dec 20, 2024
1 parent 57e8760 commit 997c62c
Show file tree
Hide file tree
Showing 12 changed files with 1,236 additions and 7 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ jobs:
- name: Install dependencies
run: go mod tidy

- name: Build
run: go build -v ./...
- name: Build Domain Proxy Server
run: go build -v -o bin/domainproxyserver cmd/server/main.go

- name: Build Domain Proxy Client
run: go build -v -o bin/domainproxyclient cmd/client/main.go

- name: Test
run: go test -v ./...
run: go test -v ./test
17 changes: 13 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
Expand All @@ -15,11 +12,23 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
/vendor/

# Go workspace file
go.work
go.work.sum

# env file
.env

# IntelliJ files
.idea/
*.ipr
*.iml
*.iws

# VS Code files
.vscode/

# Binary files
/bin/
19 changes: 19 additions & 0 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
. "org.jboss.pnc.domain-proxy/pkg/client"
"os"
"os/signal"
"syscall"
)

func main() {
domainProxyClient := NewDomainProxyClient()
ready := make(chan bool)
domainProxyClient.Start(ready)
<-ready
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
<-signals
domainProxyClient.Stop()
}
19 changes: 19 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
. "org.jboss.pnc.domain-proxy/pkg/server"
"os"
"os/signal"
"syscall"
)

func main() {
domainProxyServer := NewDomainProxyServer()
ready := make(chan bool)
domainProxyServer.Start(ready)
<-ready
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
<-signals
domainProxyServer.Stop()
}
15 changes: 15 additions & 0 deletions deploy/Dockerfile.all-in-one
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM registry.access.redhat.com/ubi9/go-toolset:1.22.5-1731639025@sha256:45170b6e45114849b5d2c0e55d730ffa4a709ddf5f58b9e810548097b085e78f as builder
USER 0
WORKDIR /work
COPY ./ .

RUN go mod tidy
RUN go build -o domainproxyserver cmd/server/main.go
RUN go build -o domainproxyclient cmd/client/main.go

FROM quay.io/konflux-ci/buildah-task:latest@sha256:5cbd487022fb7ac476cbfdea25513b810f7e343ec48f89dc6a4e8c3c39fa37a2
USER 0
WORKDIR /work/

COPY --from=builder /work/domainproxyserver /app/domain-proxy-server
COPY --from=builder /work/domainproxyclient /app/domain-proxy-client
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module org.jboss.pnc.domain-proxy

go 1.23.4

require github.com/elazarl/goproxy v0.0.0-20241218172127-ac55c7698e0d

require (
golang.org/x/net v0.32.0 // indirect
golang.org/x/text v0.21.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/elazarl/goproxy v0.0.0-20241218172127-ac55c7698e0d h1:r8DboPPvhhSMCWfmBEDoLuNvHetXH8/AZUdaRLNYgXE=
github.com/elazarl/goproxy v0.0.0-20241218172127-ac55c7698e0d/go.mod h1:3TKt+OFpElWuCtt5bphUyO97JT606j9Ffx4S2pfIcCo=
github.com/elazarl/goproxy/ext v0.0.0-20241217120900-7711dfa3811c h1:R+i10jtNSzKJKqEZAYJnR9M8y14k0zrNHqD1xkv/A2M=
github.com/elazarl/goproxy/ext v0.0.0-20241217120900-7711dfa3811c/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
110 changes: 110 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package client

import (
"fmt"
"net"
. "org.jboss.pnc.domain-proxy/pkg/common"
"time"
)

const (
Localhost = "localhost"
HttpPortKey = "DOMAIN_PROXY_HTTP_PORT"
DefaultHttpPort = 8080
HttpToDomainSocket = "HTTP <-> Domain Socket"
)

var logger = NewLogger("Domain Proxy Client")
var common = NewCommon(logger)

type DomainProxyClient struct {
sharedParams *SharedParams
httpPort int
}

func NewDomainProxyClient() *DomainProxyClient {
return &DomainProxyClient{
sharedParams: common.NewSharedParams(),
httpPort: getHttpPort(),
}
}

func (dpc *DomainProxyClient) Start(ready chan<- bool) {
sharedParams := dpc.sharedParams
logger.Println("Starting domain proxy client...")
var err error
sharedParams.Listener, err = net.Listen(TCP, fmt.Sprintf("%s:%d", Localhost, dpc.httpPort))
if err != nil {
logger.Fatalf("Failed to start HTTP server: %v", err)
}
go dpc.startClient(ready)
}

func (dpc *DomainProxyClient) startClient(ready chan<- bool) {
sharedParams := dpc.sharedParams
logger.Printf("HTTP server listening on port %d", dpc.httpPort)
ready <- true
for {
select {
case <-sharedParams.RunningContext.Done():
return
default:
if serverConnection, err := sharedParams.Listener.Accept(); err != nil {
select {
case <-sharedParams.RunningContext.Done():
return
default:
logger.Printf("Failed to accept server connection: %v", err)
}
} else {
go dpc.handleConnectionRequest(serverConnection)
}
}
}
}

func (dpc *DomainProxyClient) handleConnectionRequest(serverConnection net.Conn) {
sharedParams := dpc.sharedParams
if err := serverConnection.SetDeadline(time.Now().Add(sharedParams.IdleTimeout)); err != nil {
common.HandleSetDeadlineError(serverConnection, err)
return
}
connectionNo := sharedParams.HttpConnectionCounter.Add(1)
logger.Printf("Handling %s Connection %d", HttpToDomainSocket, connectionNo)
startTime := time.Now()
domainConnection, err := net.DialTimeout(UNIX, sharedParams.DomainSocket, sharedParams.ConnectionTimeout)
if err != nil {
logger.Printf("Failed to connect to domain socket: %v", err)
if err = serverConnection.Close(); err != nil {
common.HandleConnectionCloseError(err)
}
return
}
if err := domainConnection.SetDeadline(time.Now().Add(sharedParams.IdleTimeout)); err != nil {
common.HandleSetDeadlineError(domainConnection, err)
if err = serverConnection.Close(); err != nil {
common.HandleConnectionCloseError(err)
}
return
}
// Initiate transfer between server and domain
go func() {
common.BiDirectionalTransfer(sharedParams.RunningContext, serverConnection, domainConnection, sharedParams.ByteBufferSize, HttpToDomainSocket, connectionNo)
logger.Printf("%s Connection %d ended after %d ms", HttpToDomainSocket, connectionNo, time.Since(startTime).Milliseconds())
}()
}

func (dpc *DomainProxyClient) Stop() {
sharedParams := dpc.sharedParams
logger.Println("Shutting down domain proxy client...")
sharedParams.InitiateShutdown()
if sharedParams.Listener != nil {
if err := sharedParams.Listener.Close(); err != nil {
common.HandleListenerCloseError(err)
}
}
}

func getHttpPort() int {
return common.GetIntEnvVariable(HttpPortKey, DefaultHttpPort)
}
Loading

0 comments on commit 997c62c

Please sign in to comment.