Skip to content

Commit

Permalink
Initial working version
Browse files Browse the repository at this point in the history
* supports HTTP
* support TLS interception e.g. for HTTPS
* support CA generation via cli
* first draft of plugin API
* support commands from plugins
* includes Dockerfile
* includes basic configuration
  • Loading branch information
prskr committed Apr 1, 2020
1 parent 6012f10 commit a720b0e
Show file tree
Hide file tree
Showing 44 changed files with 2,277 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
###############
# Directories #
###############

.git/
plugins/

#########
# Files #
#########

*.out
main
inetmock
README.md
.dockerignore
.gitignore
Dockerfile
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assets/fakeFiles/* filter=lfs diff=lfs merge=lfs -text
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#########
# files #
#########

**/cov.out
**/cov-raw.out
**/*.so
*.key
*.pem
inetmock
main

###############
# directories #
###############

.idea/
43 changes: 43 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM golang:1.14-buster as build

# Create appuser.
ARG USER=inetmock
ARG USER_ID=10001

ENV CGO_ENABLED=0

# Prepare build stage - can be cached
WORKDIR /work
RUN apt-get update && \
apt-get install -y --no-install-recommends make gcc && \
adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${USER_ID}" \
"${USER}"

# Fetch dependencies
COPY Makefile go.mod go.sum ./
RUN go mod download

COPY ./ ./

# Build binary and plugins
RUN make CONTAINER=yes

# Runtime layer

FROM scratch

WORKDIR /app

COPY --from=build /etc/passwd /etc/group /etc/
COPY --from=build --chown=$USER /work/inetmock ./
COPY --from=build --chown=$USER /work/plugins/ ./plugins/

USER $USER:$USER

ENTRYPOINT ["/app/inetmock"]
64 changes: 64 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
VERSION = $(shell git describe --dirty --tags --always)
DIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
BUILD_PATH = $(DIR)/main.go
PKGS = $(shell go list ./...)
TEST_PKGS = $(shell find . -type f -name "*_test.go" -printf '%h\n' | sort -u)
GOARGS = GOOS=linux GOARCH=amd64
GO_BUILD_ARGS = -ldflags="-w -s"
GO_CONTAINER_BUILD_ARGS = -ldflags="-w -s" -a -installsuffix cgo
GO_DEBUG_BUILD_ARGS = -gcflags "all=-N -l"
BINARY_NAME = inetmock
PLUGINS = $(wildcard $(DIR)pkg/plugins/*/.)
DEBUG_PORT = 2345
DEBUG_ARGS?= --development-logs=true
INETMOCK_PLUGINS_DIRECTORY = $(DIR)plugins

.PHONY: clean all format deps compile debug test cli-cover-report html-cover-report plugins $(PLUGINS)

all: clean format compile test plugins

clean:
@find $(DIR) -type f \( -name "*.out" -or -name "*.so" \) -exec rm -f {} \;
@rm -rf $(DIR)plugins
@rm -f $(DIR)$(BINARY_NAME) $(DIR)main

format:
@go fmt $(PKGS)

deps:
@go mod tidy
@go build -v $(BUILD_PATH)

compile: deps
ifdef DEBUG
@echo 'Compiling for debugging...'
@$(GOARGS) go build $(GO_DEBUG_BUILD_ARGS) -o $(DIR)$(BINARY_NAME) $(BUILD_PATH)
else ifdef CONTAINER
@echo 'Compiling for container usage...'
@$(GOARGS) go build $(GO_CONTAINER_BUILD_ARGS) -o $(DIR)$(BINARY_NAME) $(BUILD_PATH)
else
@echo 'Compiling for normal Linux env...'
@$(GOARGS) go build $(GO_BUILD_ARGS) -o $(DIR)$(BINARY_NAME) $(BUILD_PATH)
endif

debug:
@export INETMOCK_PLUGINS_DIRECTORY
@dlv exec $(DIR)$(BINARY_NAME) \
--headless \
--listen=:2345 \
--api-version=2 \
--accept-multiclient \
-- $(DEBUG_ARGS)
test:
@go test -coverprofile=./cov-raw.out -v $(TEST_PKGS)
@cat ./cov-raw.out | grep -v "generated" > ./cov.out

cli-cover-report:
@go tool cover -func=cov.out

html-cover-report:
@go tool cover -html=cov.out -o .coverage.html

plugins: $(PLUGINS)
$(PLUGINS):
$(MAKE) -C $@
3 changes: 3 additions & 0 deletions assets/fakeFiles/default.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/fakeFiles/default.html
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/fakeFiles/default.ico
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/fakeFiles/default.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/fakeFiles/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/fakeFiles/default.txt
Git LFS file not shown
41 changes: 41 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
endpoints:
plainHttp:
handler: http_mock
listenAddress: 0.0.0.0
port: 80
options:
rules:
- pattern: ".*\\.(?i)exe"
target: ./assets/fakeFiles/sample.exe
- pattern: ".*\\.(?i)(jpg|jpeg)"
target: ./assets/fakeFiles/default.jpg
- pattern: ".*\\.(?i)png"
target: ./assets/fakeFiles/default.png
- pattern: ".*\\.(?i)gif"
target: ./assets/fakeFiles/default.gif
- pattern: ".*\\.(?i)ico"
target: ./assets/fakeFiles/default.ico
- pattern: ".*\\.(?i)txt"
target: ./assets/fakeFiles/default.txt
- pattern: ".*"
target: ./assets/fakeFiles/default.html
httpsDowngrade:
handler: tls_interceptor
listenAddress: 0.0.0.0
port: 443
options:
ecdsaCurve: P256
validity:
ca:
notBeforeRelative: 17520h
notAfterRelative: 17520h
domain:
notBeforeRelative: 168h
notAfterRelative: 168h
rootCaCert:
publicKey: ./ca.pem
privateKey: ./ca.key
certCachePath: /tmp/inetmock/
target:
ipAddress: 127.0.0.1
port: 80
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/baez90/inetmock

go 1.13

require (
github.com/spf13/cobra v0.0.6
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.4.0
go.uber.org/zap v1.14.1
golang.org/x/tools v0.0.0-20191127201027-ecd32218bd7f // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
)
Loading

0 comments on commit a720b0e

Please sign in to comment.