Skip to content

Commit

Permalink
Merge pull request #3 from muzzammilshahid/init-project
Browse files Browse the repository at this point in the history
Init project and add new WireGuard user
  • Loading branch information
muzzammilshahid authored Sep 30, 2024
2 parents fe38e0d + ac90d01 commit 020e284
Show file tree
Hide file tree
Showing 7 changed files with 589 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: wireguard CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup go
uses: actions/setup-go@v3
with:
go-version-file: 'go.mod'
cache: true
- run: go version

- name: Pull in all Go dependencies
run: |
go mod vendor
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest`
version: latest
# skip all additional steps
skip-pkg-cache: true
skip-build-cache: true

- name: Check that code is formatted via go fmt tool
run: |
if [ "$(gofmt -s -l . | grep -v vendor | wc -l)" -gt 0 ]; then
exit 1;
fi
- name: Check that go modules are in synced state
run: |
go mod tidy -v
if [ -n "$(git status --porcelain go.mod go.sum)" ]; then
echo "Go modules are dirty or not in a good state. Please run go mod tidy"
exit 1;
fi
- name: Run unit tests
run: |
make test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
183 changes: 183 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
run:
# default concurrency is a available CPU number
#concurrency: 4

# timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 5m

# exit code when at least one issue was found, default is 1
# issues-exit-code: 1

# include test files or not, default is true
tests: true

# list of build tags, all linters use it. Default is empty list.
#build-tags:
# - mytag

# which dirs to skip: issues from them won't be reported;
# can use regexp here: generated.*, regexp is applied on full path;
# default value is empty list, but default dirs are skipped independently
# from this option's value (see skip-dirs-use-default).
# "/" will be replaced by current OS file path separator to properly work
# on Windows.
#skip-dirs:
# - src/external_libs
# - autogenerated_by_my_lib

# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: false

# which files to skip: they will be analyzed, but issues from them
# won't be reported. Default value is empty list, but there is
# no need to include all autogenerated files, we confidently recognize
# autogenerated files. If it's not please let us know.
# "/" will be replaced by current OS file path separator to properly work
# on Windows.
# skip-files:
# - export_test.go

# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
# If invoked with -mod=readonly, the go command is disallowed from the implicit
# automatic updating of go.mod described above. Instead, it fails when any changes
# to go.mod are needed. This setting is most useful to check that go.mod does
# not need updates, such as in a continuous integration and testing system.
# If invoked with -mod=vendor, the go command assumes that the vendor
# directory holds the correct copies of dependencies and ignores
# the dependency descriptions in go.mod.
# modules-download-mode: readonly|release|vendor

# Allow multiple parallel golangci-lint instances running.
# If false (default) - golangci-lint acquires file lock on start.
allow-parallel-runners: false

# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
# format: colored-line-number

# print lines of code with issue, default is true
# print-issued-lines: true

# print linter name in the end of issue text, default is true
# print-linter-name: true

# make issues output unique by line, default is true
# uniq-by-line: true

# add a prefix to the output file references; default is no prefix
path-prefix: ""

# all available settings of specific linters
# @see all available linters and options here: https://golangci-lint.run/usage/linters/
linters-settings:
gci:
# Section configuration to compare against.
# Section names are case-insensitive and may contain parameters in ().
# The default order of sections is `standard > default > custom > blank > dot`,
# If `custom-order` is `true`, it follows the order of `sections` option.
# Default: ["standard", "default"]
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- prefix(github.com/xconnio) # Custom section: groups all imports with the specified Prefix.
# Skip generated files.
# Default: true
#skip-generated: false
# Enable custom order of sections.
# If `true`, make the section order the same as the order of `sections`.
# Default: false
custom-order: true

linters:
# enable the following linters
enable:
- errcheck
- gosimple
- govet
- ineffassign
- misspell
- typecheck
- asciicheck
- bidichk
- contextcheck
- decorder
- durationcheck
- errorlint
- execinquery
- exportloopref
- gci
- gochecknoglobals
- goconst
- godot
- gofmt
#- gomnd
- gosec
- nakedret
- nilerr
- testpackage
- unused
- unconvert
- unparam
- lll
# disable everything else
disable-all: true

issues:
# List of regexps of issue texts to exclude, empty list by default.
# But independently from this option we use default exclude patterns,
# it can be disabled by `exclude-use-default: false`. To list all
# excluded by default patterns execute `golangci-lint run --help`
# exclude:
# - abcdef

# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gocyclo
- errcheck
- dupl
- gosec

- path: export.*_test\.go
linters:
- testpackage
- gochecknoglobals

# benchmarks are little noisy
- path: benchmarks/*
linters:
- unparam
- unused

- path: examples/(serializers|authentications)\.go
linters:
- unused

# disable noise from errcheck
- linters:
- errcheck
text: ".AbortWithReason"

# Independently from option `exclude` we use default exclude patterns,
# it can be disabled by this option. To list all
# excluded by default patterns execute `golangci-lint run --help`.
# Default value for this option is true.
exclude-use-default: true

# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-per-linter: 0

# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
max-same-issues: 0

# Show only new issues: if there are unstaged changes or untracked files,
# only those changes are analyzed, else only changes in HEAD~ are analyzed.
# It's a super-useful option for integration of golangci-lint into existing
# large codebase. It's not practical to fix all existing issues at the moment
# of integration: much better don't allow issues in new code.
# Default is false.
new: false
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lint:
golangci-lint run
test:
go test -count=1 ./... -v
55 changes: 55 additions & 0 deletions cmd/http-service/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
"log"
"net/http"
"os"

"github.com/gin-gonic/gin"

"github.com/xconnio/wireguard-admin-service"
)

type DeviceRequest struct {
DeviceName string `json:"device_name" binding:"required"`
}

func main() {
r := gin.Default()

const qrDir = "./qr-codes"
if err := os.MkdirAll(qrDir, os.ModePerm); err != nil {
log.Fatal(err)
}
r.Static("qrs", qrDir)

const cfgDir = "./configs"
if err := os.MkdirAll(cfgDir, os.ModePerm); err != nil {
log.Fatal(err)
}
r.Static("configs", cfgDir)

r.POST("/device", func(c *gin.Context) {
var req DeviceRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "device_name is required"})
return
}

if err := wireguard_admin_service.AddUser(req.DeviceName); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

qrCode := fmt.Sprintf("http://%s/qrs/%s-client-qr.png", c.Request.Host, req.DeviceName)
config := fmt.Sprintf("http://%s/configs/client-%s.conf", c.Request.Host, req.DeviceName)

c.JSON(http.StatusOK, gin.H{
"qr_code": qrCode,
"config": config,
})
})

log.Fatalln(r.Run("0.0.0.0:8000"))
}
46 changes: 46 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module github.com/xconnio/wireguard-admin-service

go 1.22

require (
github.com/gin-gonic/gin v1.10.0
github.com/joho/godotenv v1.5.1
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6
)

require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mdlayher/genetlink v1.3.2 // indirect
github.com/mdlayher/netlink v1.7.2 // indirect
github.com/mdlayher/socket v0.4.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 020e284

Please sign in to comment.