2020-04-10 13:12:18 +00:00
|
|
|
package environment
|
2019-12-08 01:31:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-29 05:07:53 +00:00
|
|
|
"github.com/apex/log"
|
2020-04-10 13:12:18 +00:00
|
|
|
|
2019-12-08 01:31:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/pterodactyl/wings/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Configures the required network for the docker environment.
|
2020-04-10 13:12:18 +00:00
|
|
|
func ConfigureDocker(c *config.DockerConfiguration) error {
|
2019-12-08 01:31:12 +00:00
|
|
|
// Ensure the required docker network exists on the system.
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resource, err := cli.NetworkInspect(context.Background(), c.Network.Name, types.NetworkInspectOptions{})
|
|
|
|
if err != nil && client.IsErrNotFound(err) {
|
2020-05-29 05:07:53 +00:00
|
|
|
log.Info("creating missing pterodactyl0 interface, this could take a few seconds...")
|
2019-12-08 01:31:12 +00:00
|
|
|
return createDockerNetwork(cli, c)
|
|
|
|
} else if err != nil {
|
2020-05-29 05:07:53 +00:00
|
|
|
log.WithField("error", err).Fatal("failed to create required docker network for containers")
|
2019-12-08 01:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch resource.Driver {
|
|
|
|
case "host":
|
|
|
|
c.Network.Interface = "127.0.0.1"
|
|
|
|
c.Network.ISPN = false
|
|
|
|
return nil
|
|
|
|
case "overlay":
|
|
|
|
case "weavemesh":
|
|
|
|
c.Network.Interface = ""
|
|
|
|
c.Network.ISPN = true
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
c.Network.ISPN = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new network on the machine if one does not exist already.
|
|
|
|
func createDockerNetwork(cli *client.Client, c *config.DockerConfiguration) error {
|
|
|
|
_, err := cli.NetworkCreate(context.Background(), c.Network.Name, types.NetworkCreate{
|
|
|
|
Driver: c.Network.Driver,
|
|
|
|
EnableIPv6: true,
|
|
|
|
Internal: c.Network.IsInternal,
|
|
|
|
IPAM: &network.IPAM{
|
|
|
|
Config: []network.IPAMConfig{
|
|
|
|
{
|
|
|
|
Subnet: c.Network.Interfaces.V4.Subnet,
|
|
|
|
Gateway: c.Network.Interfaces.V4.Gateway,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Subnet: c.Network.Interfaces.V6.Subnet,
|
|
|
|
Gateway: c.Network.Interfaces.V6.Gateway,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Options: map[string]string{
|
|
|
|
"encryption": "false",
|
|
|
|
"com.docker.network.bridge.default_bridge": "false",
|
|
|
|
"com.docker.network.bridge.enable_icc": "true",
|
|
|
|
"com.docker.network.bridge.enable_ip_masquerade": "true",
|
|
|
|
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
|
|
|
|
"com.docker.network.bridge.name": "pterodactyl0",
|
|
|
|
"com.docker.network.driver.mtu": "1500",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.Network.Driver {
|
|
|
|
case "host":
|
|
|
|
c.Network.Interface = "127.0.0.1"
|
|
|
|
c.Network.ISPN = false
|
|
|
|
break
|
|
|
|
case "overlay":
|
|
|
|
case "weavemesh":
|
|
|
|
c.Network.Interface = ""
|
|
|
|
c.Network.ISPN = true
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
c.Network.Interface = c.Network.Interfaces.V4.Gateway
|
|
|
|
c.Network.ISPN = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|