Skip to content

Commit 98e4a83

Browse files
authored
Merge pull request #23 from projectdiscovery/docs-readme
Docs readme
2 parents ae67bbb + 8a339ee commit 98e4a83

File tree

22 files changed

+196
-235
lines changed

22 files changed

+196
-235
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232

3333
- name: Test
3434
run: go test .
35-
working-directory: cmd/simplehttpserver/
35+
working-directory: cmd/simplehttpserver
3636

3737
- name: Build
3838
run: go build .

.golangci.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ linters-settings:
4141
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
4242
# lll:
4343
# line-length: 140
44-
maligned:
45-
suggest-new: true
4644
misspell:
4745
locale: US
4846
nolintlint:
@@ -73,14 +71,12 @@ linters:
7371
- gosimple
7472
- govet
7573
- ineffassign
76-
- interfacer
77-
- maligned
7874
- misspell
7975
- nakedret
8076
- noctx
8177
- nolintlint
8278
- rowserrcheck
83-
- scopelint
79+
- exportloopref
8480
- staticcheck
8581
- structcheck
8682
- stylecheck

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ RUN apk add --no-cache git
33
RUN GO111MODULE=auto go get -u -v github.com/projectdiscovery/simplehttpserver/cmd/simplehttpserver
44

55
FROM alpine:latest
6+
RUN apk add --no-cache bind-tools ca-certificates
67
COPY --from=builder /go/bin/simplehttpserver /usr/local/bin/
78

89
ENTRYPOINT ["simplehttpserver"]

README.md

Lines changed: 109 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
[![Docker Images](https://img.shields.io/docker/pulls/projectdiscovery/simplehttpserver.svg)](https://hub.docker.com/r/projectdiscovery/simplehttpserver)
1212
[![Chat on Discord](https://img.shields.io/discord/695645237418131507.svg?logo=discord)](https://discord.gg/KECAGdH)
1313

14-
simplehttpserver is a go enhanced version of the well known python simplehttpserver.
14+
simplehttpserver is a go enhanced version of the well known python simplehttpserver with in addition a fully customizable TCP server, both supporting TLS.
1515

1616
# Resources
1717

1818
- [Features](#features)
1919
- [Usage](#usage)
2020
- [Installation Instructions](#installation-instructions)
21-
- [Running simplehttpserver](#running-simplehttpserver-in-the-current-folder )
21+
- [Running simplehttpserver](#running-simplehttpserver-in-the-current-folder)
2222
- [Thanks](#thanks)
2323

2424
# Features
@@ -28,9 +28,11 @@ simplehttpserver is a go enhanced version of the well known python simplehttpser
2828
<br>
2929
</h1>
3030

31-
- File server in arbitrary directory
32-
- Full request/response dump
33-
- Configurable ip address and listening port
31+
- HTTPS support
32+
- File server in arbitrary directory
33+
- Full request/response dump
34+
- Configurable ip address and listening port
35+
- Configurable HTTP/TCP server with customizable response via YAML template
3436

3537

3638
# Installation Instructions
@@ -70,11 +72,23 @@ simplehttpserver -h
7072

7173
This will display help for the tool. Here are all the switches it supports.
7274

73-
| Flag | Description | Example |
74-
| ------ | ---------------------------------------------------- | --------------------------------------- |
75-
| listen | Configure listening ip:port (default 127.0.0.1:8000) | simplehttpserver -listen 127.0.0.1:8000 |
76-
| path | Fileserver folder (default current directory) | simplehttpserver -path /var/docs |
77-
| v | Verbose (dump request/response, default false) | simplehttpserver -v |
75+
| Flag | Description | Example |
76+
| ----------- | -------------------------------------------------------------------- | ------------------------------------------------- |
77+
| listen | Configure listening ip:port (default 127.0.0.1:8000) | simplehttpserver -listen 127.0.0.1:8000 |
78+
| path | Fileserver folder (default current directory) | simplehttpserver -path /var/docs |
79+
| verbose | Verbose (dump request/response, default false) | simplehttpserver -v |
80+
| tcp | TCP server (default 127.0.0.1:8000) | simplehttpserver -tcp 127.0.0.1:8000 |
81+
| tls | Enable TLS for TCP server | simplehttpserver -tls |
82+
| rules | File containing yaml rules | simplehttpserver -rules rule.yaml | |
83+
| upload | Enable file upload in case of http server | simplehttpserver -upload |
84+
| https | Enable HTTPS in case of http server | simplehttpserver -https |
85+
| cert | HTTPS/TLS certificate (self generated if not specified) | simplehttpserver -cert cert.pem |
86+
| key | HTTPS/TLS certificate private key (self generated if not specified) | simplehttpserver -key cert.key |
87+
| domain | Domain name to use for the self-generated certificate | simplehttpserver -domain projectdiscovery.io |
88+
| basic-auth | Basic auth (username:password) | simplehttpserver -basic-auth user:password |
89+
| realm | Basic auth message | simplehttpserver -realm "insert the credentials" |
90+
| version | Show version | simplehttpserver -version |
91+
| silent | Show only results | simplehttpserver -silent |
7892

7993
### Running simplehttpserver in the current folder
8094

@@ -87,6 +101,91 @@ This will run the tool exposing the current directory on port 8000
87101
2021/01/11 21:41:15 [::1]:50181 "GET /favicon.ico HTTP/1.1" 404 19
88102
```
89103

104+
### Running simplehttpserver in the current folder with HTTPS
105+
106+
This will run the tool exposing the current directory on port 8000 over HTTPS with user provided certificate:
107+
108+
```sh
109+
▶ simplehttpserver -https -cert cert.pen -key cert.key
110+
2021/01/11 21:40:48 Serving . on http://0.0.0.0:8000/...
111+
2021/01/11 21:41:15 [::1]:50181 "GET / HTTP/1.1" 200 383
112+
2021/01/11 21:41:15 [::1]:50181 "GET /favicon.ico HTTP/1.1" 404 19
113+
```
114+
115+
Instead, to run with self-signed certificate and specific domain name:
116+
```sh
117+
▶ simplehttpserver -https -domain localhost
118+
2021/01/11 21:40:48 Serving . on http://0.0.0.0:8000/...
119+
2021/01/11 21:41:15 [::1]:50181 "GET / HTTP/1.1" 200 383
120+
2021/01/11 21:41:15 [::1]:50181 "GET /favicon.ico HTTP/1.1" 404 19
121+
```
122+
123+
### Running simplehttpserver with basic auth and file upload
124+
125+
This will run the tool and will request the user to enter username and password before authorizing file uploads
126+
127+
```sh
128+
▶ simplehttpserver -basic-auth root:root -upload
129+
2021/01/11 21:40:48 Serving . on http://0.0.0.0:8000/...
130+
```
131+
132+
To upload files use the following curl request with basic auth header:
133+
```sh
134+
▶ curl -v --user 'root:root' --upload-file file.txt http://localhost:8000/file.txt
135+
```
136+
137+
### Running TCP server with custom responses
138+
139+
This will run the tool as TLS TCP server and enable custom responses based on YAML templates:
140+
141+
```sh
142+
▶ simplehttpserver -rule rules.yaml -tcp -tls -domain localhost
143+
```
144+
145+
The rules are written as follows:
146+
```yaml
147+
rules:
148+
- match: regex
149+
response: response data
150+
```
151+
152+
For example to handle two different paths simulating an HTTP server or SMTP commands:
153+
```yaml
154+
rules:
155+
# HTTP Requests
156+
- match: GET /path1
157+
response: |
158+
HTTP/1.0 200 OK
159+
Server: httpd/2.0
160+
x-frame-options: SAMEORIGIN
161+
x-xss-protection: 1; mode=block
162+
Date: Fri, 16 Apr 2021 14:30:32 GMT
163+
Content-Type: text/html
164+
Connection: close
165+
166+
<HTML><HEAD><script>top.location.href='/Main_Login.asp';</script>
167+
</HEAD></HTML>
168+
- match: GET /path2
169+
response: |
170+
HTTP/1.0 404 OK
171+
Server: httpd/2.0
172+
173+
<HTML><HEAD></HEAD><BODY>Not found</BODY></HTML>
174+
# SMTP Commands
175+
- match: "EHLO example.com"
176+
response: |
177+
250-localhost Nice to meet you, [127.0.0.1]
178+
250-PIPELINING
179+
250-8BITMIME
180+
250-SMTPUTF8
181+
250-AUTH LOGIN PLAIN
182+
250 STARTTLS
183+
- match: "MAIL FROM: <noreply@example.com>"
184+
response: 250 Accepted
185+
- match: "RCPT TO: <test@example.com>"
186+
response: 250 Accepted
187+
```
188+
90189
# Thanks
91190
92191
simplehttpserver is made with 🖤 by the [projectdiscovery](https://projectdiscovery.io) team. Community contributions have made the project what it is. See the **[Thanks.md](https://github.com/projectdiscovery/simplehttpserver/blob/master/THANKS.md)** file for more details.

cmd/simplehttpserver/simplehttpserver.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
func main() {
99
// Parse the command line flags and read config files
1010
options := runner.ParseOptions()
11-
runner, err := runner.New(options)
11+
r, err := runner.New(options)
1212
if err != nil {
1313
gologger.Fatal().Msgf("Could not create runner: %s\n", err)
1414
}
1515

16-
runner.Run()
17-
runner.Close()
16+
if err := r.Run(); err != nil {
17+
gologger.Info().Msgf("%s\n", err)
18+
}
19+
defer r.Close() //nolint
1820
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module github.com/projectdiscovery/simplehttpserver
22

3-
go 1.15
3+
go 1.14
44

55
require (
66
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
77
github.com/projectdiscovery/gologger v1.1.4
8+
github.com/projectdiscovery/sslcert v0.0.0-20210416140253-8f56bec1bb5e
89
gopkg.in/yaml.v2 v2.4.0
910
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
2525
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2626
github.com/projectdiscovery/gologger v1.1.4 h1:qWxGUq7ukHWT849uGPkagPKF3yBPYAsTtMKunQ8O2VI=
2727
github.com/projectdiscovery/gologger v1.1.4/go.mod h1:Bhb6Bdx2PV1nMaFLoXNBmHIU85iROS9y1tBuv7T5pMY=
28+
github.com/projectdiscovery/sslcert v0.0.0-20210416140253-8f56bec1bb5e h1:IZa08TUGbU7I0HUb9QQt/8wuu2fPZqfnMXwWhtMxei8=
29+
github.com/projectdiscovery/sslcert v0.0.0-20210416140253-8f56bec1bb5e/go.mod h1:jSp8W5zIkNPxAqVdcoFlfv0K5cqogTe65fMinR0Fvuk=
2830
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2931
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
3032
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=

internal/runner/banner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const banner = `
1212
`
1313

1414
// Version is the current version
15-
const Version = `0.0.1`
15+
const Version = `0.0.2`
1616

1717
// showBanner is used to show the banner to the user
1818
func showBanner() {

internal/runner/options.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package runner
33
import (
44
"flag"
55
"os"
6+
"path/filepath"
67
"strings"
78

89
"github.com/projectdiscovery/gologger"
910
"github.com/projectdiscovery/gologger/levels"
1011
)
1112

13+
// Options of the tool
1214
type Options struct {
1315
ListenAddress string
1416
Folder string
@@ -91,3 +93,12 @@ func (options *Options) configureOutput() {
9193
gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent)
9294
}
9395
}
96+
97+
// FolderAbsPath of the fileserver folder
98+
func (options *Options) FolderAbsPath() string {
99+
abspath, err := filepath.Abs(options.Folder)
100+
if err != nil {
101+
return options.Folder
102+
}
103+
return abspath
104+
}

internal/runner/runner.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Runner struct {
1414
httpServer *httpserver.HTTPServer
1515
}
1616

17+
// New instance of runner
1718
func New(options *Options) (*Runner, error) {
1819
r := Runner{options: options}
1920
// Check if the process can listen on the specified ip:port
@@ -27,7 +28,7 @@ func New(options *Options) (*Runner, error) {
2728
}
2829

2930
if r.options.EnableTCP {
30-
serverTCP, err := tcpserver.New(tcpserver.Options{
31+
serverTCP, err := tcpserver.New(&tcpserver.Options{
3132
Listen: r.options.ListenAddress,
3233
TLS: r.options.TCPWithTLS,
3334
Domain: "local.host",
@@ -65,27 +66,33 @@ func New(options *Options) (*Runner, error) {
6566
return &r, nil
6667
}
6768

69+
// Run logic
6870
func (r *Runner) Run() error {
6971
if r.options.EnableTCP {
7072
gologger.Print().Msgf("Serving TCP rule based server on tcp://%s", r.options.ListenAddress)
7173
return r.serverTCP.ListenAndServe()
7274
}
7375

7476
if r.options.HTTPS {
75-
gologger.Print().Msgf("Serving %s on https://%s/...", r.options.Folder, r.options.ListenAddress)
77+
gologger.Print().Msgf("Serving %s on https://%s/", r.options.FolderAbsPath(), r.options.ListenAddress)
7678
return r.httpServer.ListenAndServeTLS()
7779
}
7880

79-
gologger.Print().Msgf("Serving %s on http://%s/...", r.options.Folder, r.options.ListenAddress)
81+
gologger.Print().Msgf("Serving %s on http://%s/", r.options.FolderAbsPath(), r.options.ListenAddress)
8082
return r.httpServer.ListenAndServe()
8183
}
8284

85+
// Close the listening services
8386
func (r *Runner) Close() error {
8487
if r.serverTCP != nil {
85-
r.serverTCP.Close()
88+
if err := r.serverTCP.Close(); err != nil {
89+
return err
90+
}
8691
}
8792
if r.httpServer != nil {
88-
r.httpServer.Close()
93+
if err := r.httpServer.Close(); err != nil {
94+
return err
95+
}
8996
}
9097
return nil
9198
}

0 commit comments

Comments
 (0)