A while ago, I was working on a project that's tightly integrated with the WireGuard go packages and found a problem - on some of the Ubuntu machines, wgctrl-go package was crashing after trying to use an in-kernel WireGuard implementation.

Usually, it would be a good idea to properly debug the issue and fix it on the client-side (or create a pull-request if it's something code-related), but in that case, the userspace implementation was working just fine, so I decided to make a fork with the fixes.

This post shows how to do such replacement quick and painless.

Forking the project

If the project's hosted on Github, you can open the repo and click on the "Fork" button.

This button creates a copy of the project under your name so that you can clone and modify it.

In my case, the original repo was https://github.com/WireGuard/wgctrl-go, and the forked repo created by Github https://github.com/crudiedo/wgctrl-go

Applying the changes

Apply the changes you wanted to, and do not forget to run unit tests (if such exist) to ensure you didn't break anything.

In my case, I replaced this part

kc, ok, err := wglinux.New()  

if err != nil {  
   return nil, err  
if ok {  
   clients = append(clients, kc)  

with that

kc, ok, err := wglinux.New()  
// if we've got an error when trying to use an in-kernel implementation, we should use userspace instead.  
if err == nil && ok {  
   clients = append(clients, kc)  

That change allows us to use the userspace implementation if the in-kernel one returns any error.

Updating the go.mod file

When the changes are ready, you need to add a new line to the beginning of the go.mod file:

replace golang.zx2c4.com/wireguard/wgctrl => {your fork url} master

In my case, I added

replace golang.zx2c4.com/wireguard/wgctrl => github.com/crudiedo/wgctrl-go master

Without that line, you won't be able to use your fork.

Run go mod tidy to automatically generate a package version for you - the version number will replace the "master" part of the line.

Now you're ready to push the changes to your fork repo.

Using your fork in the code

Use it like any other package - do go get {fork} to fetch the package and replace the original module with your fork.

In my case, I've run go get github.com/crudiedo/wgctrl-go and replaced golang.zx2c4.com/wireguard/wgctrl with github.com/crudiedo/wgctrl-go

And that's it.

Thanks (: