proproxy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e01e4f15866b2b60d8d521d2eb363bd5d74f7df04c92dc44c0830e6e7eb659f5
4
+ data.tar.gz: a8343e20379481da8c8a6266091067ecc2090dac92ce001abefeca48e9651513
5
+ SHA512:
6
+ metadata.gz: 5f445cb856f886c319f34c9aed0b25a72e94201be70aabc11a4ba0a3fb397277467bd8bfdb77ed05bb0bc516c26b7d31fac3992e21186edc5ebb01861021e696
7
+ data.tar.gz: 0f18051dd07efb444a98a0526f179344ea2861dcec53a7d46e953082e60e24e1c1fbc0a46e43016c8c6356612f34f1afbc4a07995e58df6254706e35ba8c7e19
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/bundle
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at yohei.senju@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in proproxy.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ proproxy (0.1.0)
5
+ sshkit
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ method_source (0.9.0)
13
+ net-scp (1.2.1)
14
+ net-ssh (>= 2.6.5)
15
+ net-ssh (5.0.2)
16
+ pry (0.11.3)
17
+ coderay (~> 1.1.0)
18
+ method_source (~> 0.9.0)
19
+ rake (10.5.0)
20
+ rspec (3.8.0)
21
+ rspec-core (~> 3.8.0)
22
+ rspec-expectations (~> 3.8.0)
23
+ rspec-mocks (~> 3.8.0)
24
+ rspec-core (3.8.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-expectations (3.8.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-mocks (3.8.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-support (3.8.0)
33
+ sshkit (1.17.0)
34
+ net-scp (>= 1.1.2)
35
+ net-ssh (>= 2.8.0)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler (~> 1.16)
42
+ proproxy!
43
+ pry
44
+ rake (~> 10.0)
45
+ rspec (~> 3.0)
46
+
47
+ BUNDLED WITH
48
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 100010
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Proproxy
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/proproxy`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'proproxy'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install proproxy
21
+
22
+ ## Usage
23
+
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/proproxy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
38
+
39
+ ## Code of Conduct
40
+
41
+ Everyone interacting in the Proproxy project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/proproxy/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "proproxy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Proproxy
2
+ VERSION = "0.1.0"
3
+ end
data/lib/proproxy.rb ADDED
@@ -0,0 +1,141 @@
1
+ require "proproxy/version"
2
+ require 'pry'
3
+ require 'sshkit'
4
+
5
+ class InvalidServerNameError < StandardError ; end
6
+
7
+ module Proproxy
8
+ class Server
9
+ # TODO:
10
+ # AVAILABLE_OS_NAME = [
11
+ # :ubuntu,
12
+ # :centos
13
+ # ]
14
+
15
+ include SSHKit::DSL
16
+
17
+ def initialize(os_name, ip, port, options={})
18
+ # TODO: enable to choose even if the server is not ubuntu
19
+ # unless AVAILABLE_OS_NAME.include? os_name
20
+ # raise InvalidServerNameError.new 'invalid os name'
21
+ # end
22
+
23
+ ssh_path = if options[:ssh_path].nil?
24
+ '~/.ssh/id_rsa'
25
+ else
26
+ options[:ssh_path]
27
+ end
28
+
29
+ username = if options[:username].nil?
30
+ 'root'
31
+ else
32
+ options[:username]
33
+ end
34
+
35
+ SSHKit.config.output_verbosity = Logger::DEBUG
36
+
37
+ @remote_host = SSHKit::Host.new(ip)
38
+ @remote_host.user = username
39
+ @remote_host.ssh_options = {
40
+ keys: [ ssh_path ],
41
+ auth_methods: %w(publickey)
42
+ }
43
+ end
44
+
45
+ def provision
46
+ on @remote_host do
47
+ execute 'sudo apt-get update -y'
48
+ execute 'sudo apt-get install squid -y'
49
+ execute 'mkdir /etc/sysconfig/'
50
+ execute 'touch /etc/sysconfig/iptables'
51
+ end
52
+ copy_template
53
+ end
54
+
55
+ def restart_squid
56
+ stop_squid
57
+ start_squid
58
+ end
59
+
60
+ def update_ip_table(ip_v4, port, with_ssh_port: true)
61
+ new_tonnel = "-A FWINPUT -p tcp -m tcp --dport #{port} -s #{ip_v4} -j ACCEPT"
62
+ new_port = "http_port #{port}"
63
+ new_src = "acl myacl src #{ip_v4}/255.255.255.255"
64
+
65
+ remove_last_2_line
66
+ on @remote_host do
67
+ execute "echo #{new_tonnel} >> /etc/sysconfig/iptables"
68
+ execute "echo #{new_port} >> /etc/squid/squid.conf"
69
+ execute "echo #{new_src} >> /etc/squid/squid.conf"
70
+ end
71
+ add_icmp_host_prohibited_line
72
+ add_last_commit_line_command
73
+
74
+ if with_ssh_port
75
+ ssh_tonnel = "-A FWINPUT -p tcp -m tcp --dport 22 -s #{ip_v4} -j ACCEPT"
76
+ ssh_port = "http_port 22"
77
+ remove_last_2_line
78
+ on @remote_host do
79
+ execute "echo #{ssh_tonnel} >> /etc/sysconfig/iptables"
80
+ execute "echo #{ssh_port} >> /etc/squid/squid.conf"
81
+ end
82
+ add_icmp_host_prohibited_line
83
+ add_last_commit_line_command
84
+ end
85
+ configure_ip_table
86
+ restart_squid
87
+ end
88
+
89
+ def configure_ip_table
90
+ on @remote_host do
91
+ execute 'iptables-restore < /etc/sysconfig/iptables'
92
+ end
93
+ end
94
+
95
+ def stop_squid
96
+ on @remote_host do
97
+ execute 'service squid stop'
98
+ end
99
+ end
100
+
101
+ def start_squid
102
+ on @remote_host do
103
+ execute 'service squid start'
104
+ end
105
+ end
106
+
107
+ def clear_squid_cache
108
+ on @remote_host do
109
+ execute 'squid -z'
110
+ end
111
+ end
112
+
113
+ private
114
+
115
+ def remove_last_2_line
116
+ on @remote_host do
117
+ execute 'head -n -2 /etc/sysconfig/iptables > /etc/sysconfig/tmp_iptables ; mv /etc/sysconfig/tmp_iptables /etc/sysconfig/iptables'
118
+ end
119
+ end
120
+
121
+ def add_last_commit_line_command
122
+ on @remote_host do
123
+ execute 'echo COMMIT >> /etc/sysconfig/iptables'
124
+ end
125
+ end
126
+
127
+ def add_icmp_host_prohibited_line
128
+ on @remote_host do
129
+ execute 'echo -A FWINPUT -j REJECT --reject-with icmp-host-prohibited >> /etc/sysconfig/iptables'
130
+ end
131
+ end
132
+
133
+ def copy_template
134
+ root = Dir.pwd
135
+ on @remote_host do
136
+ upload! "#{root}/templates/iptables", '/etc/sysconfig/iptables'
137
+ upload! "#{root}/templates/squid.conf", '/etc/squid/squid.conf'
138
+ end
139
+ end
140
+ end
141
+ end
data/proproxy.gemspec ADDED
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "proproxy/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "proproxy"
8
+ spec.version = Proproxy::VERSION
9
+ spec.authors = ["100010"]
10
+ spec.email = ["yohei.senju@gmail.com"]
11
+
12
+ spec.summary = %q{Write a short summary, because RubyGems requires one.}
13
+ spec.description = %q{Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/100010/proproxyi"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency 'sshkit'
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.16"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ spec.add_development_dependency 'pry'
39
+ end
@@ -0,0 +1,12 @@
1
+ *filter
2
+ :INPUT ACCEPT [0:0]
3
+ :FORWARD ACCEPT [0:0]
4
+ :OUTPUT ACCEPT [0:0]
5
+ :FWINPUT - [0:0]
6
+ -A INPUT -j FWINPUT
7
+ -A FORWARD -j FWINPUT
8
+ -A FWINPUT -i lo -j ACCEPT
9
+ -A FWINPUT -p icmp --icmp-type any -j ACCEPT
10
+ -A FWINPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
11
+ -A FWINPUT -j REJECT --reject-with icmp-host-prohibited
12
+ COMMIT