Skip to content

Commit

Permalink
Rework packaging
Browse files Browse the repository at this point in the history
Packaging is now up to date and follows the recommended project layout.
See https://github.com/golang-standards/project-layout
  • Loading branch information
brmzkw committed Apr 27, 2020
1 parent c158ec4 commit 80b6383
Show file tree
Hide file tree
Showing 353 changed files with 167,471 additions and 115 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
bin/
src/hooktftp
./hooktftp
test/files/
test/fixtures/
test/shell/
test/web/
test/hooktftp.pid
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ before_script:
- sudo apt-get install -q -y atftp python

script:
- make build
- make test

go:
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ VOLUME /var/lib/tftpboot
COPY . /go/src/github.com/tftp-go-team/hooktftp
WORKDIR /go/src/github.com/tftp-go-team/hooktftp

RUN make
RUN make build

RUN echo '\
user: nobody\n\
Expand All @@ -19,4 +19,4 @@ hooks:\n\
regexp: ^.*$\n\
template: /var/lib/tftpboot/$0' > /etc/hooktftp.yml

CMD ./src/hooktftp -v
CMD ./hooktftp -v
28 changes: 13 additions & 15 deletions MAINTAINERS.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
**This guide is intended for hooktftp maintainers. If you are not a maintainer,
you probably want to check out the [documentation](README.md) instead.**
**This guide is intended for hooktftp maintainers. If you are not a maintainer, you probably want to check out the [documentation](README.md) instead.**

## Package release HOWTO

You made some updates on hooktftp and want to release a new version for your
users? Make sure to complete this todo list.
You made some updates on hooktftp and want to release a new version for your users? Make sure to complete this todo list.


### Docker image
### Make sure debian package is still working

Build the Docker image:
A debian/ directory is provided to create a .deb package. To make sure it is still working, run the following commands:

$> docker build -t tftpgoteam/hooktftp:latest .
$> make shell
#> apt-get install -y build-essential debhelper golang-go
#> dpkg-buildpackage -us -uc
#> cd ..
#> dpkg -i hooktftp_.deb

A docker image needs to be pushed on the [Docker
hub](https://hub.docker.com/r/tftpgoteam/hooktftp/). Ping @brmzkw on Github or
send him an email at castets.j - at - gmail.com to ask him to make the release.
If you want to do it by yourself, ask him to grant you the permissions to do
so.
### Docker image

Push the image:
Build and release the [Docker image](https://hub.docker.com/r/tftpgoteam/hooktftp/):

$> docker push tftpgoteam/hooktftp:latest
$> make release-docker-image

Alternatively, the command `make release` will build and push the image for you.
Ping @brmzkw on Github or send him an email at castets.j - at - gmail.com to ask him to make the release. If you want to do it by yourself, ask him to grant you the permissions to do so.
29 changes: 16 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
.PHONY: build
build:
$(MAKE) -C src
go build ./cmd/hooktftp

.PHONY: build-docker-image
build-docker-image:
docker build -t tftpgoteam/hooktftp:latest .

.PHONY: release-docker-image
release-docker-image: build-docker-image
docker push tftpgoteam/hooktftp:latest

.PHONY: shell
shell: build-docker-image
docker run --rm -ti -v $(shell pwd):/go/src/github.com/tftp-go-team/hooktftp/ -w /go/src/github.com/tftp-go-team/hooktftp --name hooktftp hooktftp-shell bash

.PHONY: test
test: build
$(MAKE) -C src test
$(MAKE) -C test all
go test ./...

.PHONY: clean
clean:
$(MAKE) -C src clean
go clean
$(MAKE) -C test clean

.PHONY: gox
gox:
$(MAKE) -C src gox

shell:
docker build -t hooktftp-shell .
docker run --rm -ti -v $(shell pwd):/go/src/github.com/tftp-go-team/hooktftp -w /go/src/github.com/tftp-go-team/hooktftp --name hooktftp hooktftp-shell bash

release:
docker build -t tftpgoteam/hooktftp:latest .
docker push tftpgoteam/hooktftp:latest
$(MAKE) ./cmd/hooktftp
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
# hooktftp

Hooktftp is a dynamic read-only TFTP server. It's dynamic in a sense it is
executes hooks matched on read requests (RRQ) instead of reading files from
the file system. Hooks are matched with regular expressions and on match
hooktftp will execute a script, issue a HTTP GET request or just reads the file
from the filesystem.
Hooktftp is a dynamic read-only TFTP server. It's dynamic in a sense it is executes hooks matched on read requests (RRQ) instead of reading files from the file system. Hooks are matched with regular expressions and on match hooktftp will execute a script, issue a HTTP GET request or just reads the file from the filesystem.

This is inspired by [puavo-tftp]. It's written in Go in the hope of being faster
and more stable.
This is inspired by [puavo-tftp]. It's written in Go in the hope of being faster and more stable.

## Usage

hooktftp [-v] [config]

Config will be read from `/etc/hooktftp.yml` by default. Verbose option `-v`
print log to stderr insteadof syslog.
Config will be read from `/etc/hooktftp.yml` by default. Verbose option `-v` print log to stderr insteadof syslog.

## Configuration

Expand All @@ -36,9 +30,7 @@ Hooks consists of following keys:
- Hook is executed when this regexp matches the requested path
- `template`: A template where the regexp is expanded

Regexp can be expanded to the template using the `$0`, `$1`, `$2` etc.
variables. `$0` is the full matched regexp and rest are the matched regexp
groups.
Regexp can be expanded to the template using the `$0`, `$1`, `$2` etc. variables. `$0` is the full matched regexp and rest are the matched regexp groups.

### Example

Expand Down Expand Up @@ -79,7 +71,7 @@ hooks:
### Compiling from sources
[Install Go][] (1.8 or later), make sure you have git and bazaar too.
Install Go, make sure you have git and bazaar too.
Assuming you've successfully set up GOPATH and have GOPATH/bin on your path, simply:
```
Expand Down
7 changes: 7 additions & 0 deletions cmd/hooktftp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import hooktftp "github.com/tftp-go-team/hooktftp/internal"

func main() {
hooktftp.HookTFTP()
}
File renamed without changes.
1 change: 0 additions & 1 deletion debian/README.source

This file was deleted.

2 changes: 2 additions & 0 deletions debian/files
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hooktftp_1.0.0_amd64.buildinfo utils extra
hooktftp_1.0.0_amd64.deb utils extra
24 changes: 24 additions & 0 deletions debian/hooktftp.debhelper.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
dh_update_autotools_config
dh_auto_configure
dh_auto_build
dh_auto_test
dh_prep
dh_installdirs
dh_auto_install
dh_install
dh_installdocs
dh_installchangelogs
dh_installinit
dh_perl
dh_link
dh_strip_nondeterminism
dh_compress
dh_fixperms
dh_missing
dh_strip
dh_makeshlibs
dh_shlibdeps
dh_installdeb
dh_gencontrol
dh_md5sums
dh_builddeb
4 changes: 2 additions & 2 deletions debian/hooktftp.install
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
src/hooktftp /usr/sbin/
src/hooktftp.yml /etc/
hooktftp /usr/sbin/
configs/hooktftp.yml /etc/
3 changes: 3 additions & 0 deletions debian/hooktftp.substvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
shlibs:Depends=libc6 (>= 2.3.2)
misc:Depends=
misc:Pre-Depends=
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/tftp-go-team/hooktftp

go 1.14

require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/tftp-go-team/libgotftp v0.0.0-20160115164016-99de0d58e1b9
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
launchpad.net/goyaml v0.0.0-20140305200416-000000000051
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/tftp-go-team/libgotftp v0.0.0-20160115164016-99de0d58e1b9 h1:OM0wxbfcSN8wHp4nvvxpW9Horlvkp7FgndGNXhjoP8s=
github.com/tftp-go-team/libgotftp v0.0.0-20160115164016-99de0d58e1b9/go.mod h1:cfvBegOY4cEfGffFkN7AdLZtFY1jo6gXMHCB15Ir72k=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f h1:gWF768j/LaZugp8dyS4UwsslYCYz9XgFxvlgsn0n9H8=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 h1:Izowp2XBH6Ya6rv+hqbceQyw/gSGoXfH/UPoTGduL54=
launchpad.net/gocheck v0.0.0-20140225173054-000000000087/go.mod h1:hj7XX3B/0A+80Vse0e+BUHsHMTEhd0O4cpUHr/e/BUM=
launchpad.net/goyaml v0.0.0-20140305200416-000000000051 h1:pgtOxpWGtZwl5VEGtXB3s+VPsO3QzsZPg4/AS0Ag9HI=
launchpad.net/goyaml v0.0.0-20140305200416-000000000051/go.mod h1:HivH2TsqnIPviNUySFrCE+pbgbJCjrnMj2QH74o4y+w=
1 change: 1 addition & 0 deletions src/config/parse_yaml.go → internal/config/parse_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"errors"

"launchpad.net/goyaml"
)

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/drop_privileges.go → internal/drop_privileges.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package hooktftp

import (
"os/user"
Expand Down
9 changes: 5 additions & 4 deletions src/hooks/compile.go → internal/hooks/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"io"
"regexp"

"github.com/tftp-go-team/hooktftp/src/config"
"github.com/tftp-go-team/hooktftp/src/logger"
"github.com/tftp-go-team/hooktftp/src/regexptransform"
"github.com/tftp-go-team/libgotftp/src"
"github.com/tftp-go-team/hooktftp/internal/config"
"github.com/tftp-go-team/hooktftp/internal/logger"
"github.com/tftp-go-team/hooktftp/internal/regexptransform"

tftp "github.com/tftp-go-team/libgotftp/src"
)

var NO_MATCH = regexptransform.NO_MATCH
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/hooks/file_hook.go → internal/hooks/file_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"os"
"regexp"

"github.com/tftp-go-team/hooktftp/src/config"
"github.com/tftp-go-team/libgotftp/src"
"github.com/tftp-go-team/hooktftp/internal/config"
tftp "github.com/tftp-go-team/libgotftp/src"
)

var pathEscape = regexp.MustCompile("\\.\\.\\/")
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/hooks_test.go → internal/hooks/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"net/http/httptest"
"testing"

"github.com/tftp-go-team/hooktftp/src/config"
"github.com/tftp-go-team/libgotftp/src"
"github.com/tftp-go-team/hooktftp/internal/config"
tftp "github.com/tftp-go-team/libgotftp/src"
)

type hookTestCase struct {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/http_hook.go → internal/hooks/http_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"net/http"
"net/url"

"github.com/tftp-go-team/hooktftp/src/config"
"github.com/tftp-go-team/libgotftp/src"
"github.com/tftp-go-team/hooktftp/internal/config"
tftp "github.com/tftp-go-team/libgotftp/src"
)

var HTTPHook = HookComponents{
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/shell_hook.go → internal/hooks/shell_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"regexp"

"github.com/google/shlex"
"github.com/tftp-go-team/hooktftp/src/config"
"github.com/tftp-go-team/libgotftp/src"
"github.com/tftp-go-team/hooktftp/internal/config"
tftp "github.com/tftp-go-team/libgotftp/src"
)

// Borrowed from Ruby
Expand Down
20 changes: 10 additions & 10 deletions src/hooktftp.go → internal/hooktftp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package hooktftp

import (
"flag"
Expand All @@ -12,10 +12,10 @@ import (
"syscall"
"time"

"github.com/tftp-go-team/hooktftp/src/config"
"github.com/tftp-go-team/hooktftp/src/hooks"
"github.com/tftp-go-team/hooktftp/src/logger"
"github.com/tftp-go-team/libgotftp/src"
"github.com/tftp-go-team/hooktftp/internal/config"
"github.com/tftp-go-team/hooktftp/internal/hooks"
"github.com/tftp-go-team/hooktftp/internal/logger"
tftp "github.com/tftp-go-team/libgotftp/src"
)

var HOOKS []hooks.Hook
Expand Down Expand Up @@ -117,7 +117,7 @@ func handleRRQ(res *tftp.RRQresponse) {
}

if err := res.WriteOACK(); err != nil {
logger.Err("Failed to write OACK", err)
logger.Err("Failed to write OACK: %v", err)
return
}

Expand Down Expand Up @@ -154,7 +154,7 @@ func handleRRQ(res *tftp.RRQresponse) {
logger.Info("Sent %v bytes in %v %f MB/s\n", totalBytes, took, speed)
}

func main() {
func HookTFTP() {

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "\nUsage: %s [-v] [config]\n", os.Args[0])
Expand Down Expand Up @@ -189,15 +189,15 @@ func main() {
}

for _, hookDef := range conf.HookDefs {
logger.Notice("Compiling hook %s", hookDef)
logger.Notice("Compiling hook %v", hookDef)

// Create new hookDef variable for the hookDef pointer for each loop
// iteration. Go reuses the hookDef variable and if we pass pointer to
// that terrible things happen.
newPointer := hookDef
hook, err := hooks.CompileHook(&newPointer)
if err != nil {
logger.Crit("Failed to compile hook %s: %s", hookDef, err)
logger.Crit("Failed to compile hook %v: %v", hookDef, err)
return
}
HOOKS = append(HOOKS, hook)
Expand All @@ -219,7 +219,7 @@ func main() {
return
}

logger.Notice("Listening on %s:%d", conf.Host, conf.Port)
logger.Notice("Listening on %v:%v", conf.Host, conf.Port)

if conf.User != "" {
err := DropPrivileges(conf.User)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
"strconv"

"github.com/tftp-go-team/hooktftp/src/config"
"github.com/tftp-go-team/hooktftp/internal/config"
)

var NO_MATCH = errors.New("No match")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"testing"

"github.com/tftp-go-team/hooktftp/src/config"
"github.com/tftp-go-team/hooktftp/internal/config"
)

func dummyEscape(s string, extraArgs config.HookExtraArgs) (string, error) {
Expand Down
Loading

0 comments on commit 80b6383

Please sign in to comment.