Skip to content

Commit

Permalink
Reorganised code.
Browse files Browse the repository at this point in the history
  • Loading branch information
tecarter94 committed Nov 28, 2024
1 parent ecb62e7 commit 599899d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ dev-image-controller:

dev: dev-image-controller
cd java-components && mvn clean install -Dlocal -DskipTests -Ddev
cd domain-proxy && go build -o domain-proxy-server domain_proxy_server.go common.go && go build -o domain-proxy-client domain_proxy_client.go common.go
cd domain-proxy && go build -o domain-proxy-server github.com/redhat-appstudio/jvm-build-service/domain-proxy/cmd/server && go build -o domain-proxy-client github.com/redhat-appstudio/jvm-build-service/domain-proxy/cmd/client
docker build . -f domain-proxy/Dockerfile.local -t quay.io/$(QUAY_USERNAME)/hacbs-jvm-domain-proxy:"$${JBS_QUAY_IMAGE_TAG:-dev}"
docker push quay.io/$(QUAY_USERNAME)/hacbs-jvm-domain-proxy:"$${JBS_QUAY_IMAGE_TAG:-dev}"

Expand Down
4 changes: 2 additions & 2 deletions domain-proxy/Dockerfile.all-in-one
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ USER 0
WORKDIR /work
COPY ./ .

RUN go build -o domain-proxy-server domain_proxy_server.go common.go
RUN go build -o domain-proxy-client domain_proxy_client.go common.go
RUN go build -o domain-proxy-server github.com/redhat-appstudio/jvm-build-service/domain-proxy/cmd/server
RUN go build -o domain-proxy-client github.com/redhat-appstudio/jvm-build-service/domain-proxy/cmd/client

FROM quay.io/konflux-ci/buildah-task:latest@sha256:5cbd487022fb7ac476cbfdea25513b810f7e343ec48f89dc6a4e8c3c39fa37a2
USER 0
Expand Down
19 changes: 19 additions & 0 deletions domain-proxy/cmd/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
. "github.com/redhat-appstudio/jvm-build-service/domain-proxy/pkg/client"
. "github.com/redhat-appstudio/jvm-build-service/domain-proxy/pkg/common"
"os"
"os/signal"
"syscall"
)

func main() {
InitLogger("Domain Proxy Client")
client := NewDomainProxyClient(GetDomainSocket(), GetServerHttpPort(), GetByteBufferSize(), GetConnectionTimeout(), GetIdleTimeout())
client.Start()
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
<-signals
client.Stop()
}
25 changes: 25 additions & 0 deletions domain-proxy/cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
. "github.com/redhat-appstudio/jvm-build-service/domain-proxy/pkg/common"
. "github.com/redhat-appstudio/jvm-build-service/domain-proxy/pkg/server"
"os"
"os/signal"
"syscall"
)

func main() {
InitLogger("Domain Proxy Server")
server := NewDomainProxyServer(GetDomainSocket(),
GetByteBufferSize(),
GetConnectionTimeout(),
GetIdleTimeout(),
GetCsvEnvVariable(ProxyTargetWhitelistKey, DefaultProxyTargetWhitelist),
GetCsvEnvVariable(InternalNonProxyHostsKey, DefaultInternalNonProxyHosts), // TODO Implement Non-proxy logic
)
server.Start()
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
<-signals
server.Stop()
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package main
package client

import (
"fmt"
. "github.com/redhat-appstudio/jvm-build-service/domain-proxy/pkg/common"
"net"
"os"
"os/signal"
"sync/atomic"
"syscall"
"time"
)

Expand Down Expand Up @@ -94,13 +92,3 @@ func (dpc *DomainProxyClient) Stop() {
func GetServerHttpPort() int {
return GetIntEnvVariable(ServerHttpPortKey, DefaultServerHttpPort)
}

func main() {
InitLogger("Domain Proxy Client")
client := NewDomainProxyClient(GetDomainSocket(), GetServerHttpPort(), GetByteBufferSize(), GetConnectionTimeout(), GetIdleTimeout())
client.Start()
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
<-signals
client.Stop()
}
12 changes: 6 additions & 6 deletions domain-proxy/common.go → domain-proxy/pkg/common/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package common

import (
"errors"
Expand Down Expand Up @@ -32,11 +32,11 @@ func BiDirectionalTransfer(leftConnection, rightConnection net.Conn, byteBufferS
defer CloseConnection(leftConnection, rightConnection, connectionType, connectionNo)
done := make(chan struct{}, 2)
if err := leftConnection.SetDeadline(time.Now().Add(idleTimeout)); err != nil {
handleSetDeadlineError(leftConnection, err)
HandleSetDeadlineError(leftConnection, err)
return
}
if err := rightConnection.SetDeadline(time.Now().Add(idleTimeout)); err != nil {
handleSetDeadlineError(rightConnection, err)
HandleSetDeadlineError(rightConnection, err)
return
}
go Transfer(leftConnection, rightConnection, done, byteBufferSize, idleTimeout, connectionType, connectionNo)
Expand All @@ -56,19 +56,19 @@ func Transfer(sourceConnection, targetConnection net.Conn, done chan struct{}, b
return
} else if n > 0 {
if err = sourceConnection.SetReadDeadline(time.Now().Add(idleTimeout)); err != nil {
handleSetDeadlineError(sourceConnection, err)
HandleSetDeadlineError(sourceConnection, err)
return
}
if err = targetConnection.SetWriteDeadline(time.Now().Add(idleTimeout)); err != nil {
handleSetDeadlineError(targetConnection, err)
HandleSetDeadlineError(targetConnection, err)
return
}
Logger.Printf("%d bytes transferred for %s connection %d", n, connectionType, connectionNo)
}
}
}

func handleSetDeadlineError(connection net.Conn, err error) {
func HandleSetDeadlineError(connection net.Conn, err error) {
Logger.Printf("Failed to set deadline: %v", err)
if err = connection.Close(); err != nil {
HandleConnectionCloseError(err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package main
package server

import (
"bufio"
"fmt"
. "github.com/redhat-appstudio/jvm-build-service/domain-proxy/pkg/common"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"
)

const (
HttpPort = 80
HttpsPort = 443
ProxyTargetWhitelistKey = "PROXY_TARGET_WHITELIST"
DefaultProxyTargetWhitelist = "repo.maven.apache.org,repository.jboss.org,packages.confluent.io,jitpack.io,repo.gradle.org,plugins.gradle.org"
DefaultProxyTargetWhitelist = "gariscus.com,neverssl.com,repo1.maven.org,repo.maven.apache.org,repository.jboss.org,packages.confluent.io,jitpack.io,repo.gradle.org,plugins.gradle.org"
InternalNonProxyHostsKey = "INTERNAL_NON_PROXY_HOSTS"
DefaultInternalNonProxyHosts = "localhost"
DomainSocketToHttp = "Domain Socket <-> HTTP"
Expand Down Expand Up @@ -83,7 +82,7 @@ func (dps *DomainProxyServer) startServer() {

func (dps *DomainProxyServer) handleConnectionRequest(domainConnection net.Conn) {
if err := domainConnection.SetDeadline(time.Now().Add(dps.idleTimeout)); err != nil {
handleSetDeadlineError(domainConnection, err)
HandleSetDeadlineError(domainConnection, err)
return
}
reader := bufio.NewReader(domainConnection)
Expand All @@ -97,7 +96,7 @@ func (dps *DomainProxyServer) handleConnectionRequest(domainConnection net.Conn)
}
writer := &responseWriter{connection: domainConnection}
if err = domainConnection.SetDeadline(time.Now().Add(dps.idleTimeout)); err != nil {
handleSetDeadlineError(domainConnection, err)
HandleSetDeadlineError(domainConnection, err)
return
}
if request.Method == http.MethodConnect {
Expand Down Expand Up @@ -245,19 +244,3 @@ func (rw *responseWriter) WriteHeader(statusCode int) {
Logger.Printf("Failed to write headers to connection: %v", err)
}
}

func main() {
InitLogger("Domain Proxy Server")
server := NewDomainProxyServer(GetDomainSocket(),
GetByteBufferSize(),
GetConnectionTimeout(),
GetIdleTimeout(),
GetCsvEnvVariable(ProxyTargetWhitelistKey, DefaultProxyTargetWhitelist),
GetCsvEnvVariable(InternalNonProxyHostsKey, DefaultInternalNonProxyHosts), // TODO Implement Non-proxy logic
)
server.Start()
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
<-signals
server.Stop()
}

0 comments on commit 599899d

Please sign in to comment.