Created
February 9, 2017 15:41
-
-
Save mattn/b0f6fbda65f590e313d39c6c8e57e153 to your computer and use it in GitHub Desktop.
Remove Windows Firewall Rules which is generated by "go run"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"path/filepath" | |
"strings" | |
"github.com/go-ole/go-ole" | |
"github.com/go-ole/go-ole/oleutil" | |
) | |
func main() { | |
pattern := filepath.Join(os.TempDir(), "go-build") | |
ole.CoInitialize(0) | |
unk, err := oleutil.CreateObject("HNetCfg.FwPolicy2") | |
if err != nil { | |
log.Fatal(err) | |
} | |
dsp, err := unk.QueryInterface(ole.IID_IDispatch) | |
if err != nil { | |
log.Fatal(err) | |
} | |
rules := oleutil.MustGetProperty(dsp, "Rules").ToIDispatch() | |
err = oleutil.ForEach(rules, func(v *ole.VARIANT) error { | |
rule := v.ToIDispatch() | |
appname := oleutil.MustGetProperty(rule, "Applicationname").ToString() | |
if strings.HasPrefix(strings.ToLower(appname), strings.ToLower(pattern)) { | |
_, rerr := oleutil.CallMethod(rules, "Remove", appname) | |
if rerr != nil { | |
log.Fatal(rerr) | |
} | |
fmt.Println(appname) | |
} | |
return nil | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment