<commit_before>package hook

import (
	"database/sql"
	"fmt"
	"net"
	"os"
	"os/exec"
	"strings"

	"cmd/user"
	"db"
	"utils"

	"github.com/cjey/slog"
)

func Connect(env map[string]string, dconfPath string) {
	dev := env["dev"]
	username := env["username"]
	if len(username) == 0 {
		username = env["common_name"]
	}
	ifconfig_local := env["ifconfig_local"]
	ifconfig_pool_remote_ip := env["ifconfig_pool_remote_ip"]
	ifconfig_netmask := env["ifconfig_netmask"]
	ifconfig_pool_netmask := env["ifconfig_pool_netmask"]
	a_ip := env["untrusted_ip"]
	a_port := env["untrusted_port"]

	f, err := os.OpenFile(dconfPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
	if err != nil {
		slog.Emerg(err.Error())
		os.Exit(1)
	}
	defer f.Close()

	name, device := user.StdUsername(username)
	if len(name) == 0 {
		slog.Emergf("Invalid username format: %s", username)
		os.Exit(1)
	}
	if len(device) == 0 {
		device = "default"
	}
	show := name + "%" + device

	// fetch ifconfig
	DB := db.Get()
	var static, netmask, gateway, route, dns string
	err = DB.QueryRow(`
        select ip,netmask,gateway,routes,dns from ifconfig
        where username=? and device=? and ovpn_dev=?
    `, name, device, dev).Scan(&static, &netmask, &gateway, &route, &dns)
	if err != nil && err != sql.ErrNoRows {
		slog.Emerg(err.Error())
		os.Exit(1)
	}
	if err == sql.ErrNoRows {
		err = DB.QueryRow(`
            select ip,netmask,gateway,routes,dns from ifconfig
            where username=? and device='' and ovpn_dev=?
        `, name, dev).Scan(&static, &netmask, &gateway, &route, &dns)
		if err != nil && err != sql.ErrNoRows {
			slog.Emerg(err.Error())
			os.Exit(1)
		}
	}

	remote_ip := ifconfig_pool_remote_ip
	if len(static) > 0 {
		remote_ip = static
	}
	if len(remote_ip) == 0 {
		netmask = ""
	} else if len(netmask) == 0 {
		if len(ifconfig_pool_netmask) > 0 {
			netmask = ifconfig_pool_netmask
		} else if len(ifconfig_netmask) > 0 {
			netmask = ifconfig_netmask
		} else {
			netmask = "255.255.255.255"
		}
	}
	if len(gateway) == 0 {
		if defgw := env["oum_gateway"]; len(defgw) > 0 {
			gateway = defgw
		} else {
			gateway = ifconfig_local
		}
	}
	var dnss []string
	if len(dns) > 0 {
		dnss = utils.CSVSet(dns)<commit_msg>fix: read correct static ip config<commit_after>package hook

import (
	"database/sql"
	"fmt"
	"net"
	"os"
	"os/exec"
	"strings"

	"cmd/user"
	"db"
	"utils"

	"github.com/cjey/slog"
)

func Connect(env map[string]string, dconfPath string) {
	dev := env["dev"]
	username := env["username"]
	if len(username) == 0 {
		username = env["common_name"]
	}
	ifconfig_local := env["ifconfig_local"]
	ifconfig_pool_remote_ip := env["ifconfig_pool_remote_ip"]
	ifconfig_netmask := env["ifconfig_netmask"]
	ifconfig_pool_netmask := env["ifconfig_pool_netmask"]
	a_ip := env["untrusted_ip"]
	a_port := env["untrusted_port"]

	f, err := os.OpenFile(dconfPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
	if err != nil {
		slog.Emerg(err.Error())
		os.Exit(1)
	}
	defer f.Close()

	name, device := user.StdUsername(username)
	if len(name) == 0 {
		slog.Emergf("Invalid username format: %s", username)
		os.Exit(1)
	}
	if len(device) == 0 {
		device = "default"
	}
	show := name + "%" + device

	// fetch ifconfig
	DB := db.Get()

	rows, err := DB.Query(`
        select device,ovpn_dev,ip,netmask,gateway,routes,dns from ifconfig
        where username=?
    `, name)
	if err != nil && err != sql.ErrNoRows {
		slog.Emerg(err.Error())
		os.Exit(1)
	}

	var static, netmask, gateway, route, dns string
	if err == nil {
		var hit, a, b, c, d []string
		hit = []string{static, netmask, gateway, route, dns}
		err = db.RangeRows(rows, func() error {
			var dvc, cdev string
			err := rows.Scan(&dvc, &dev, &static, &netmask, &gateway, &route, &dns)
			if err != nil {
				return err
			}
			if cdev == dev && dvc == device {
				a = []string{static, netmask, gateway, route, dns}
			} else if cdev == dev && dvc == "" {
				b = []string{static, netmask, gateway, route, dns}
			} else if cdev == "" && dvc == device {
				c = []string{static, netmask, gateway, route, dns}
			} else if cdev == "" && dvc == "" {
				d = []string{static, netmask, gateway, route, dns}
			}
			return nil
		})
		if err != nil {
			slog.Emergf(err.Error())
			os.Exit(1)
		}
		switch {
		case len(a) > 0:
			hit = a
		case len(b) > 0:
			hit = b
		case len(c) > 0:
			hit = c
		case len(d) > 0:
			hit = d
		}
		static, netmask, gateway, route, dns = hit[0], hit[1], hit[2], hit[3], hit[4]
	}

	remote_ip := ifconfig_pool_remote_ip
	if len(static) > 0 {
		remote_ip = static
	}
	if len(remote_ip) == 0 {
		netmask = ""
	} else if len(netmask) == 0 {
		if len(ifconfig_pool_netmask) > 0 {
			netmask = ifconfig_pool_netmask
		} else if len(ifconfig_netmask) > 0 {
			netmask = ifconfig_netmask
		} else {
			netmask = "255.255.255.255"
		}
	}
	if len(gateway) == 0 {
		if defgw := env["oum_gateway"]; len(defgw) > 0 {
			gateway = defgw
		} else {
			gateway = ifconfig_local
		}
	}
	var dnss []string
	if len(dns) > 0 {
		dnss = utils.CSVSet(dns)
{'fraction_non_alphanumeric': 0.1634117178158629, 'fraction_numerical': 0.014149063371861299, 'mean_word_length': 3.3987730061349692, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6309132', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2038, 'n_words': 457}