We study the optimal financing and dividend distribution problem with restricted dividend rates in a diffusion type surplus model, where the drift and volatility coefficients are general functions of the level of surplus and the external environment regime. The environment regime is modeled by a Markov process. Both capital injection and dividend payments incur expenses. The objective is to maximize the expectation of the total discounted dividends minus the total cost of the capital injection. We prove that it is optimal to inject capital only when the surplus tends to fall below 0 and to pay out dividends at the maximal rate when the surplus is at or above the threshold, dependent on the environment regime.