内容纲要

import (
    "gopkg.in/routeros.v2"
)

func AddRouterOSNat(address, username, password, lanIP, wanIP string) (string, error) {

    c, err := routeros.Dial(address, username, password)
    if err != nil {
        return "", err
    }
    defer c.Close()

    sentence := make([]string, 6)
    sentence[0] = "/ip/firewall/nat/add"
    sentence[1] = "=chain=dstnat"
    sentence[2] = strings.Join([]string{"=dst-address=", wanIP}, "")
    sentence[3] = "=action=netmap"
    sentence[4] = strings.Join([]string{"=to-addresses=", lanIP}, "")
    sentence[5] = strings.Join([]string{"=comment=", lanIP, "-netmap"}, "")

    reply, err := c.RunArgs(sentence)
    if err != nil {
        //fmt.Println(reply.String())
        fmt.Println(err.Error())
        return "", err
    }

    fmt.Println(reply.String())

    sentence[1] = "=chain=srcnat"
    sentence[2] = strings.Join([]string{"=src-address=", wanIP}, "")
    sentence[3] = "=action=src-nat"
    sentence[4] = strings.Join([]string{"=to-addresses=", lanIP}, "")
    sentence[5] = strings.Join([]string{"=comment=", lanIP, "-nat"}, "")

    reply, err = c.RunArgs(sentence)
    if err != nil {
        //fmt.Println(reply.String())
        fmt.Println(err.Error())
        return "", err
    }

    // fmt.Println(reply.String())

    return reply.String(), nil
}

func UpdateRouterOSNat(address, username, password, lanIP, wanIP string) (string, error) {

    c, err := routeros.Dial(address, username, password)
    if err != nil {
        return "", err
    }
    defer c.Close()

    search_sentence := make([]string, 3)
    search_sentence[0] = "/ip/firewall/nat/print"
    search_sentence[1] = strings.Join([]string{"?comment=", lanIP, "-netmap"}, "")
    search_sentence[2] = "=.proplist=.id"

    reply, err := c.RunArgs(search_sentence)
    if err != nil {
        //fmt.Println(reply.String())
        fmt.Println(err.Error())
        return "", err
    }

    if len(reply.Re) == 0 {
        return "", nil
    }

    fmt.Println(reply.Re[0].Map[".id"])

    update_sentence := make([]string, 3)
    update_sentence[0] = "/ip/firewall/nat/set"
    update_sentence[1] = strings.Join([]string{"=.id=", reply.Re[0].Map[".id"]}, "")
    update_sentence[2] = strings.Join([]string{"=dst-address=", wanIP}, "")

    reply, err = c.RunArgs(update_sentence)
    if err != nil {
        //fmt.Println(reply.String())
        fmt.Println(err.Error())
        return "", err
    }

    search_sentence[1] = strings.Join([]string{"?comment=", lanIP, "-nat"}, "")
    search_sentence[2] = "=.proplist=.id"

    reply, err = c.RunArgs(search_sentence)
    if err != nil {
        //fmt.Println(reply.String())
        fmt.Println(err.Error())
        return "", err
    }

    if len(reply.Re) == 0 {
        return "", nil
    }

    fmt.Println(reply.Re[0].Map[".id"])

    update_sentence[1] = strings.Join([]string{"=.id=", reply.Re[0].Map[".id"]}, "")
    update_sentence[2] = strings.Join([]string{"=src-address=", wanIP}, "")

    reply, err = c.RunArgs(update_sentence)
    if err != nil {
        //fmt.Println(reply.String())
        fmt.Println(err.Error())
        return "", err
    }

    return reply.String(), nil
}

func RemoveRouterOSNat(address, username, password, lanIP string) (string, error) {

    c, err := routeros.Dial(address, username, password)
    if err != nil {
        return "", err
    }
    defer c.Close()

    search_sentence := make([]string, 3)
    search_sentence[0] = "/ip/firewall/nat/print"
    search_sentence[1] = strings.Join([]string{"?comment=", lanIP, "-netmap"}, "")
    search_sentence[2] = "=.proplist=.id"

    reply, err := c.RunArgs(search_sentence)
    if err != nil {
        //fmt.Println(reply.String())
        fmt.Println(err.Error())
        return "", err
    }

    if len(reply.Re) == 0 {
        return "", nil
    }

    fmt.Println(reply.Re[0].Map[".id"])

    remove_sentence := make([]string, 2)
    remove_sentence[0] = "/ip/firewall/nat/remove"
    remove_sentence[1] = strings.Join([]string{"=.id=", reply.Re[0].Map[".id"]}, "")

    reply, err = c.RunArgs(remove_sentence)
    if err != nil {
        //fmt.Println(reply.String())
        fmt.Println(err.Error())
        return "", err
    }

    search_sentence[1] = strings.Join([]string{"?comment=", lanIP, "-nat"}, "")
    search_sentence[2] = "=.proplist=.id"

    reply, err = c.RunArgs(search_sentence)
    if err != nil {
        //fmt.Println(reply.String())
        fmt.Println(err.Error())
        return "", err
    }

    if len(reply.Re) == 0 {
        return "", nil
    }

    fmt.Println(reply.Re[0].Map[".id"])

    remove_sentence[1] = strings.Join([]string{"=.id=", reply.Re[0].Map[".id"]}, "")

    reply, err = c.RunArgs(remove_sentence)
    if err != nil {
        //fmt.Println(reply.String())
        fmt.Println(err.Error())
        return "", err
    }

    return reply.String(), nil
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注