-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6682adc
commit cbf1020
Showing
9 changed files
with
504 additions
and
266 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* ZDNS Copyright 2022 Regents of the University of Michigan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"sync" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type FileInputHandler struct { | ||
filepath string | ||
} | ||
|
||
func NewFileInputHandler(filepath string) *FileInputHandler { | ||
return &FileInputHandler{ | ||
filepath: filepath, | ||
} | ||
} | ||
|
||
func (h *FileInputHandler) FeedChannel(in chan<- interface{}, wg *sync.WaitGroup) error { | ||
defer close(in) | ||
defer (*wg).Done() | ||
|
||
var f *os.File | ||
if h.filepath == "" || h.filepath == "-" { | ||
f = os.Stdin | ||
} else { | ||
var err error | ||
f, err = os.Open(h.filepath) | ||
if err != nil { | ||
log.Fatalf("unable to open input file: %v", err) | ||
} | ||
} | ||
s := bufio.NewScanner(f) | ||
for s.Scan() { | ||
in <- s.Text() | ||
} | ||
if err := s.Err(); err != nil { | ||
log.Fatalf("input unable to read file: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
type FileOutputHandler struct { | ||
filepath string | ||
} | ||
|
||
func NewFileOutputHandler(filepath string) *FileOutputHandler { | ||
return &FileOutputHandler{ | ||
filepath: filepath, | ||
} | ||
} | ||
|
||
func (h *FileOutputHandler) WriteResults(results <-chan string, wg *sync.WaitGroup) error { | ||
defer (*wg).Done() | ||
|
||
var f *os.File | ||
if h.filepath == "" || h.filepath == "-" { | ||
f = os.Stdout | ||
} else { | ||
var err error | ||
f, err = os.OpenFile(h.filepath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) | ||
if err != nil { | ||
log.Fatalf("unable to open output file: %v", err) | ||
} | ||
defer f.Close() | ||
} | ||
for n := range results { | ||
f.WriteString(n + "\n") | ||
} | ||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* ZDNS Copyright 2022 Regents of the University of Michigan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"sync" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type StreamInputHandler struct { | ||
reader io.Reader | ||
} | ||
|
||
func NewStreamInputHandler(r io.Reader) *StreamInputHandler { | ||
return &StreamInputHandler{ | ||
reader: r, | ||
} | ||
} | ||
|
||
func (h *StreamInputHandler) FeedChannel(in chan<- interface{}, wg *sync.WaitGroup) error { | ||
defer close(in) | ||
defer (*wg).Done() | ||
|
||
s := bufio.NewScanner(h.reader) | ||
for s.Scan() { | ||
in <- s.Text() | ||
} | ||
if err := s.Err(); err != nil { | ||
log.Fatalf("unable to read input stream: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
type StreamOutputHandler struct { | ||
writer io.Writer | ||
} | ||
|
||
func NewStreamOutputHandler(w io.Writer) *StreamOutputHandler { | ||
return &StreamOutputHandler{ | ||
writer: w, | ||
} | ||
} | ||
|
||
func (h *StreamOutputHandler) WriteResults(results <-chan string, wg *sync.WaitGroup) error { | ||
defer (*wg).Done() | ||
for n := range results { | ||
io.WriteString(h.writer, n+"\n") | ||
} | ||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//go:build linux || darwin | ||
// +build linux darwin | ||
|
||
/* | ||
* ZDNS Copyright 2020 Regents of the University of Michigan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"syscall" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func ulimitCheck(maxOpenFiles uint64) { | ||
var rLimit syscall.Rlimit | ||
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) | ||
if err != nil { | ||
log.Fatal("Failed to fetch ulimit ", err) | ||
} | ||
|
||
if maxOpenFiles > rLimit.Cur { | ||
log.Warn("Current nofile limit (", rLimit.Cur, ") lower than maximum connection count (", maxOpenFiles, "), trying to update.") | ||
|
||
rLimit.Max = maxOpenFiles | ||
rLimit.Cur = maxOpenFiles | ||
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) | ||
if err != nil { | ||
log.Fatal("Error setting nofile limit to ", rLimit.Cur, ": ", err) | ||
} | ||
log.Info("Updated nofile limit to ", rLimit.Cur) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//go:build !linux && !darwin | ||
// +build !linux,!darwin | ||
|
||
/* | ||
* ZDNS Copyright 2020 Regents of the University of Michigan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
func ulimitCheck(maxOpenFiles uint64) { | ||
// fallback for ulimit check on unsupported platform | ||
} |
Oops, something went wrong.